【问题标题】:Java jdom xml parsingJava jdom xml解析
【发布时间】:2013-05-31 00:59:54
【问题描述】:

这是我使用 java 的第一天,我尝试为我的网站构建一个小的 xml 解析器,这样我就可以在我的 sitemaps.xml 上有一个干净的外观。我使用的代码是这样的

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.util.List;


import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

class downloadxml {
   public static void main(String[] args) throws IOException {

       String str = "http://www.someurl.info/sitemap.xml";
       URL url = new URL(str);
       InputStream is = url.openStream();
       int ptr = 0;
       StringBuilder builder = new StringBuilder();
       while ((ptr = is.read()) != -1) {
           builder.append((char) ptr);
       }
       String xml = builder.toString();

       org.jdom2.input.SAXBuilder saxBuilder = new SAXBuilder();
       try {
           org.jdom2.Document doc = saxBuilder.build(new StringReader(xml));
           System.out.println(xml);
           Element xmlfile = doc.getRootElement();
           System.out.println("ROOT -->"+xmlfile);
           List list = xmlfile.getChildren("url");
           System.out.println("LIST -->"+list);
       } catch (JDOMException e) {
           // handle JDOMExceptio n
       } catch (IOException e) {
           // handle IOException
       }

       System.out.println("===========================");

   }
}

当代码通过时

System.out.println(xml);

我得到了 xml 站点地图的清晰打印。说到:

System.out.println("ROOT -->"+xmlfile);

输出:

ROOT -->[Element: <urlset [Namespace: http://www.sitemaps.org/schemas/sitemap/0.9]/>]

它还找到根元素。但是由于某种原因,当脚本应该用于孩子时,它会返回一个空打印:

System.out.println("LIST -->"+list);

输出:

LIST -->[]

我应该以其他方式做什么?有什么可以得到孩子的指针吗?

XML 看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
          <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
            xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
               <url>
                   <loc>http://www.image.url</loc>
                   <image:image>
                     <image:loc>http://www.image.url/image.jpg</image:loc>
                   </image:image>
                   <changefreq>daily</changefreq>
                 </url>
                <url>
            </urlset>

【问题讨论】:

    标签: java xml xml-parsing jdom jdom-2


    【解决方案1】:

    一天之内你已经走了很长一段路。

    简短的回答,您忽略了 XML 文档的命名空间。换行:

    List list = xmlfile.getChildren("url");
    

    Namespace ns = Namespace.getNamespace("http://www.sitemaps.org/schemas/sitemap/0.9");
    List list = xmlfile.getChildren("url", ns);
    

    为方便起见,您可能还希望将整个构建过程简化为:

    org.jdom2.Document doc = saxBuilder.build("http://www.someurl.info/sitemap.xml");
    

    【讨论】:

    【解决方案2】:

    我的评论与上述类似,但使用 catch 子句,当输入的 xml 不是“格式正确”时会显示很好的消息。这里的输入是一个xml文件。

    File file = new File("adr781.xml");
    SAXBuilder builder = new SAXBuilder(false);
        try {
            Document doc = builder.build(file);
            Element root = doc.getRootElement();
        } catch (JDOMException e) {
            say(file.getName() + " is not well-formed.");
            say(e.getMessage());
        } catch (IOException e) {
            say("Could not check " + file.getAbsolutePath());
            say(" because " + e.getMessage());
        }
    

    【讨论】:

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