【问题标题】:XML Parsing using JDOM fails使用 JDOM 解析 XML 失败
【发布时间】:2012-05-05 13:34:17
【问题描述】:

我正在使用 JDOM 解析器来解析我使用 feedrinse 创建的 XML。

XML 位于:http://www.feedrinse.com/services/rinse/?rinsedurl=396ac3b0142bb6360003c8dbac4f8d47

我的代码是解析每个标题、链接、pubDate 元素并将它们存储在我发送到前端的新 XML 中。这是我的代码:

String temp = "http://www.feedrinse.com/services/rinse/?rinsedurl=396ac3b0142bb6360003c8dbac4f8d47";
String XMLstr="<FEED>";

SAXBuilder builder = new SAXBuilder();
URL url= new URL(temp);
Document doc = null;                
 try{
    doc=builder.build(url);             
    Element root = doc.getRootElement();
    //List children = root.getChildren();
    List list = root.getChildren("item");
    XMLstr+="<children>"+list.size()+"</children>";
    for (int i = 0; i < list.size(); i++) {
           Element node = (Element) list.get(i);
           String title=node.getChildText("title").toString();
           XMLstr+="<title>"+title+"</title>";
           String link=node.getChildText("link").toString();
           XMLstr+="<link>"+link+"</link>";
           String desc=node.getChildText("description").toString();
           XMLstr+="<desc>"+desc+"</desc>";
           String pubDate=node.getChildText("pubDate").toString();
           XMLstr+="<pubDate>"+pubDate+"</pubDate>";           
        }
    }
catch(Exception e)
    {
    out.println(e);
    }
    XMLstr+="</FEED>";

但是,它没有正确解析。起初它总是将孩子的大小显示为 0。请建议我在做什么错误以及如何纠正错误。谢谢。

【问题讨论】:

    标签: xml parsing xml-parsing jdom


    【解决方案1】:

    XML 具有以下结构

    <rss>
      <channel>
        ...
        <item></item>
        <item></item>
    

    例如Element root = doc.getRootElement() 将返回没有任何 item 子级的 rss 元素。

    编辑:尝试以下行

    List list = root.getChild("channel").getChildren("item");
    

    【讨论】:

    • 我可以使用 root.getChild().getChildren("item") 吗?
    • 这只蚂蚁也能正常工作codeElement root = doc.getRootElement();元素通道=root.getChild(); //列出孩子 = root.getChildren(); List list = channel.getChildren("item");code
    • 谢谢您,先生.. 即使我尝试过类似的方法,但可能是它有一些语法问题。我尝试阅读 API 文档,它说使用 getChild 的命名空间。所以我有点困惑。您能否分享一个很好的解析资源。
    • 实际上,我也使用了 API 文档 (jdom.org/docs/apidocs/org/jdom2/Element.html) 作为参考。 getChild(ren) 方法有多个版本,有命名空间和没有命名空间。
    【解决方案2】:

    JDOM2 也让事情变得更简单......

    public static void main(String[] args) throws MalformedURLException, JDOMException, IOException {
        final URL url = new URL("http://www.feedrinse.com/services/rinse/?rinsedurl=396ac3b0142bb6360003c8dbac4f8d47");
        SAXBuilder sbuilder = new SAXBuilder();
        Document doc = sbuilder.build(url);
        XPathExpression<Element> itemxp = XPathFactory.instance().compile("/rss/channel/item", Filters.element());
    
        Element feed = new Element("FEED");
    
        List<Element> items = itemxp.evaluate(doc);
        Element kidcount = new Element("children");
        kidcount.setText("" + items.size());
        feed.addContent(kidcount);
        for (Element item : items) {
    
            addItem(feed, item, "title");
            addItem(feed, item, "link");
            addItem(feed, item, "description");
            addItem(feed, item, "pubDate");
        }
    
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
    
        //xout.output(doc, System.out);
        xout.output(feed, System.out);
    }
    
    private static void addItem(Element feed, Element item, String name) {
        Element emt = item.getChild(name);
        if (emt == null) {
            return;
        }
        feed.addContent(emt.clone());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多