2012-09-05 48 views
2

我使用com.google.gwt.xml.client。*。在客戶端使用GWT構建RSS

我需要在XML的開頭和項目塊內創建標題元素。但是當我選擇createElement(「title」)和appendChild(title)時,我最終只得到一個元素標題。

private void formXMLToSend(){ 
    Document doc = XMLParser.createDocument(); 
    Element root = doc.createElement("rss"); 
    root.setAttribute("version", "2.0"); 
    doc.appendChild(root); 

    Element chl = doc.createElement("channel"); 
    root.appendChild(chl); 

    Element title = doc.createElement("title"); 
    title.appendChild(doc.createTextNode("sitename")); 
    Element link = doc.createElement("link"); 
    link.appendChild(doc.createTextNode("http://mysite.com")); 

    chl.appendChild(title); 
    chl.appendChild(link); 

    for(int i = 0; i < items.size(); i++){ 
    Element item = doc.createElement("item"); 
    Element subTitle = doc.createElement("title"); 
    title.appendChild(doc.createTextNode(items.get(i).getName())); 
    Element descr = doc.createElement("description"); 
    descr.appendChild(doc.createTextNode(items.get(i).getText())); 
    item.appendChild(title); 
    item.appendChild(descr); 
    chl.appendChild(item,link); 
    } 

    Window.alert(doc.toString()); 
} 

回答

1
public String createRss2(List<Item> items){ 
    Document doc = (Document) XMLParser.createDocument(); 
    Element root = doc.createElement("rss"); 
    root.setAttribute("version", "2.0"); 
    doc.appendChild(root); 

    Element chl = doc.createElement("channel"); 
    root.appendChild(chl); 

    Element title = doc.createElement("title of the feed"); 
    title.appendChild(doc.createTextNode("sitename")); 
    chl.appendChild(title); 

    Element link = doc.createElement("link"); 
    link.appendChild(doc.createTextNode("http://mysite.com")); 
    chl.appendChild(link); 

    Element description = doc.createElement("description"); 
    description.appendChild(doc.createTextNode("some description")); 
    chl.appendChild(description); 

    Element lang = doc.createElement("language"); 
    lang.appendChild(doc.createTextNode("en")); 
    chl.appendChild(lang); 

    Element copyright = doc.createElement("copyright"); 
    copyright.appendChild(doc.createTextNode("nicolas schwarzentrub")); 
    chl.appendChild(copyright); 

    Element pubDate = doc.createElement("pubDate"); 
    pubDate.appendChild(doc.createTextNode(DateTimeFormat.getFormat("EEE, dd MMM yyyy hh:mm:ss ZZZZ").format(new Date()))); 
    chl.appendChild(pubDate); 


    Element image = doc.createElement("image"); 
    Element imageUrl = doc.createElement("url"); 
    imageUrl.appendChild(doc.createTextNode("https://ssl.gstatic.com/gb/images/j_e6a6aca6.png")); 
    Element imageTitle = doc.createElement("title"); 
    imageTitle.appendChild(doc.createTextNode("some title of image")); 
    Element imageLink = doc.createElement("link"); 
    imageLink.appendChild(doc.createTextNode("http://www.google.ch/")); 

    image.appendChild(imageUrl); 
    image.appendChild(imageTitle); 
    image.appendChild(imageLink); 
    chl.appendChild(image); 

    for(Item obj : items) 
    { 
     Element item = doc.createElement("item"); 

     Element itemTitle = doc.createElement("title"); 
     itemTitle.appendChild(doc.createTextNode(obj.getName())); 
     item.appendChild(itemTitle); 


     Element itemDescription = doc.createElement("description"); 
     itemDescription.appendChild(doc.createTextNode(obj.getText())); 
     item.appendChild(itemDescription); 

     Element itemLink = doc.createElement("link"); 
     itemLink.appendChild(doc.createTextNode(obj.getLink())); 
     item.appendChild(itemLink); 

     Element itemAuthor = doc.createElement("author"); 
     itemLink.appendChild(doc.createTextNode(obj.getAuthor())); 
     item.appendChild(itemAuthor); 
     //and others if needed .. 
     Element itemPubDate = doc.createElement("pubDate"); 
     itemPubDate.appendChild(doc.createTextNode(DateTimeFormat.getFormat("EEE, dd MMM yyyy hh:mm:ss ZZZZ").format(new Date()))); 
     item.appendChild(itemPubDate); 

     //append this item to the link element 
     chl.appendChild(item); 
    } 


    return doc.toString(); 
    } 

要測試,把在以下一個GWTTestCase

List<Item> theItems = new ArrayList<TestXML.Item>(); 
    for(int i= 0; i<3 ; i++) 
    { 
    theItems.add(new Item("item "+i, "description of item "+i, "n.s","http://www.stackoverflow.com")); 
    } 

    System.out.println(createRss2(theItems)); 

下面上面的代碼片段將產生低於

<rss version="2.0"> 
<channel> 
    <title of the feed>sitename</title of the feed> 
    <link>http://mysite.com</link> 
    <description>some description</description> 
    <language>en</language> 
    <copyright>nicolas schwarzentrub</copyright> 
    <pubDate>Wed, 05 Sep 2012 02:15:07 GMT+02:00</pubDate> 
    <image> 
     <url>https://ssl.gstatic.com/gb/images/j_e6a6aca6.png</url> 
     <title>some title of image</title> 
     <link>http://www.google.ch/</link> 
    </image> 
    <item> 
     <title>item 0</title> 
     <description>description of item 0</description> 
     <link>http://www.stackoverflow.comn.s</link> 
     <author /> 
     <pubDate>Wed, 05 Sep 2012 02:15:07 GMT+02:00</pubDate> 
    </item> 
    <item> 
     <title>item 1</title> 
     <description>description of item 1</description> 
     <link>http://www.stackoverflow.comn.s</link> 
     <author /> 
     <pubDate>Wed, 05 Sep 2012 02:15:07 GMT+02:00</pubDate> 
    </item> 
    <item> 
     <title>item 2</title> 
     <description>description of item 2</description> 
     <link>http://www.stackoverflow.comn.s</link> 
     <author /> 
     <pubDate>Wed, 05 Sep 2012 02:15:07 GMT+02:00</pubDate> 
    </item> 
</channel> 

+0

感謝例如XML ,我在代碼中發現了變量名中的錯誤。 – dos65