如何在java中使用DOM解析xml

时间:2014-04-29 14:04:40

标签: java xml parsing dom xml-parsing

我使用此代码解析java中的xml数据但是给我一个错误:

    Informations info=new Informations();
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept", "application/XML");
    String xml="";
    xml = readUrl(conn);     
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        Document dom = db.parse(is);
        Element root = dom.getDocumentElement();
        NodeList items = root.getElementsByTagName("deal");


        //----titre
        NodeList titre = dom.getElementsByTagName("titre");
        Element line = (Element) titre.item(0);
        info.setTitre(getCharacterDataFromElement(line));
        System.out.println("Titre: " + info.getTitre());
       //----reduction
        NodeList reduction = dom.getElementsByTagName("reduction");
        line = (Element) reduction.item(0);
        info.setReduction(getCharacterDataFromElement(line));
        System.out.println("Reduction: " + info.getReduction());

这是xml数据:

<xml version="1.0" encoding="UTF-8">
<deals>
 <deal>
  <type>Occasion</type>
  <datedebutdeal>0000-00-00 00:00:00</datedebutdeal>
  <datefindeal>2014-04-30 00:00:00</datefindeal>  
  <reduction>25.93</reduction>
  <titre>A4</titre>
 </deal>
</deals>

它在代码中给出了这个错误:

  Document dom = db.parse(is);

这是错误:

[Fatal Error] :2069:1: XML document structures must start and end within the same   entity.
org.xml.sax.SAXParseException: XML document structures must start and end within the same entity.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)

感谢您的帮助。

4 个答案:

答案 0 :(得分:1)

xml的第一行必须是<?xml version="1.0" encoding="UTF-8"?>,否则会被视为标记,从而导致错误。

答案 1 :(得分:0)

XML的第一行不正确。将其更改为<?xml version="1.0" encoding="UTF-8"?>

答案 2 :(得分:0)

您可以通过添加xml = xml.replaceFirst("<xml version=\"1.0\" encoding=\"UTF-8\">", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");来替换第一行xml 在xml = readUrl(conn);之后。

答案 3 :(得分:0)

两个解决方法: 在句子之前:Document dom = db.parse(is);您应该读取字符串中的整个输入流并删除无效行。 否则,如果服务器无法解决该错误,您可以用

替换第一行