在客户端使用GWT构建RSS

时间:2012-09-05 08:52:15

标签: java xml gwt

我使用com.google.gwt.xml.client。*。

我需要在XML的开头和项目块内创建标题元素。但是当我选择createElement(“title”)和appendChild(title)时,我最终只得到一个Element标题。

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 个答案:

答案 0 :(得分: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));

以下代码片段将生成

下面的xml
<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>