使用DOM解析器从XML中读取注释

时间:2019-06-10 18:24:47

标签: java xml-parsing

当我尝试从XML文件读取Comment's时,当它通过循环时,来自两个元素的Comment's打印两次。它应该在第一次迭代中打印第一个元素注释,并在下次迭代中打印第二个元素注释。如果不清楚,请附加预期的输出和实际的输出以供参考。

XML代码:

`
<shipments>
<shipment id="011">
<department>XXXX</department>
<!--  Product: XXXXX-->
</shipment>`

`

代码:

公共类主要{

public static void main(String[] args) throws SAXException,
IOException, ParserConfigurationException, XMLStreamException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

      // Ignores all the comments described in the XML File
      factory.setIgnoringComments(false);
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse(new File("Details.xml"));
    doc.getDocumentElement().normalize(); 

    NodeList ShipmentList = doc.getElementsByTagName("shipment");

    for (int i = 0; i < ShipmentList.getLength(); i++)
    {
     Node node = ShipmentList.item(i);
             if (node.getNodeType() == Node.ELEMENT_NODE)
     {
           Element eElement = (Element) node; 
        XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream("shipmentDetails_1.xml")); 
         while (xr.hasNext()) {
             if (xr.next() == XMLStreamConstants.COMMENT) {                                     
                 String comment = xr.getText();         
                 System.out.print("Comments: ");
                 System.out.println(comment);

             } }


     }
    }
}

}

预期输出:

评论: 产品:笔记本电脑

评论: 产品:手机

输出我所得到的:

评论:产品:笔记本电脑
评论:产品:手机

评论:产品:笔记本电脑
评论:产品:手机

2 个答案:

答案 0 :(得分:1)

要从XML声明中获取值,请在Document上调用以下方法:


已更新

要在<shipment>元素内查找和打印注释,请迭代该元素的子节点并查找类型为COMMENT_NODE的节点,将其强制转换为Comment,然后打印值的getData()

for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
    if (child.getNodeType() == Node.COMMENT_NODE) {
        Comment comment = (Comment) child;
        System.out.println("COMMENTS : " + comment.getData());
    }
}

要澄清:此处使用的node来自问题代码。您也可以使用eElement代替node。没什么。

答案 1 :(得分:-1)

要获取XML声明和注释,建议将文件加载为文本文件并通过正则表达式进行解析。例如:

    String file = new String(Files.readAllBytes(Paths.get("shipmentDetails_1.xml")), StandardCharsets.UTF_8);

    Pattern pattern = Pattern.compile("<!--([\\s\\S]*?)-->");
    Matcher matcher = pattern.matcher(file);
    while (matcher.find()) {
        System.out.println("COMMENTS: " + matcher.group(1));
    }

    Pattern pattern2 = Pattern.compile("<\\?xml([\\s\\S]*?)\\?>");
    Matcher matcher2 = pattern2.matcher(file);
    while (matcher2.find()) {
        System.out.println("DECLARATION: " + matcher2.group(1));
    }