在java中读取xml

时间:2014-09-05 12:46:58

标签: java xml

我正在尝试使用java -

从xml中读取以下项目

Xml是 -

 <?xml version="1.0" encoding="utf-8"?>
<Item1>
  <Field>content1</Field>   
   <Row>
    <Item>3</Item>
    <id>33</id>
    <content>&lt;script type="text/javascript" xml:space="preserve"&gt;
&lt;/script&gt;&lt;span style="FONT-WEIGHT: bold"&gt;Access to $data&lt;/span&gt;&lt;br /&gt;
The $data is&amp;#160;&amp;#160;the$word oneas, $companyname's  our website - $link.&lt;br /&gt;
&lt;br /&gt;
recommend on your $name ase  or visit one of our $name dealer - $dealer.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="FONT-WEIGHT: bold"&gt;Meters&lt;/span&gt;&lt;br /&gt;accurate. earlier..&lt;br /&gt;
</content>
  </Row>
  </Item1>

我能够阅读内容,但它也带有特殊字符。

在阅读内容时,想要删除特殊字符,例如&amp; lt,它应该是&lt;。

有人可以建议我如何继续。

我正在阅读xml,如下所示 -

public class ReadXml {

  public static void main(String argv[]) {
    try { 
    File fXmlFile = new File("test.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    /*InputSource is = new InputSource(fXmlFile);
    is.setEncoding("UTF-8");*/
    Document doc = dBuilder.parse(fXmlFile);    
    doc.getDocumentElement().normalize();
    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    NodeList nList = doc.getElementsByTagName("Row");
    System.out.println("----------------------------");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        System.out.println("\nCurrent Element :" + nNode.getNodeName());
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            //System.out.println("Staff id : " + eElement.getAttribute("item"));
            System.out.println("Id : " + eElement.getElementsByTagName("id").item(0).getTextContent());
        }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
  }

}

1 个答案:

答案 0 :(得分:0)

您可以使用java xerces库。我使用了SAX解析器,它自动转换&amp; lt;到&lt;和&amp; gt; &GT; http://xerces.apache.org/xerces-j/