简体   繁体   English

从SAX迁移到DOM需要一些帮助。 解析XML(java)

[英]Need some help moving from SAX to DOM. Parsing XML (java)

So I've managed to parse my XML document successfully using SAX, and I am now trying to parse the XML document using DOM (resulting in the same output as achieved using SAX). 因此,我设法使用SAX成功解析了我的XML文档,现在我正尝试使用DOM解析XML文档(其结果与使用SAX所获得的输出相同)。

I've managed to edit most of the SAX code to work in DOM. 我已经设法编辑了大多数SAX代码以在DOM中工作。

For example with SAX in start and end element I used: 例如,我在开始和结束元素中使用了SAX:

if (qName.equals("Name")){...}

and with DOM in case Node.ELEMENT_NODE I've just edited this to: 并使用DOM(如果是Node.ELEMENT_NODE的话),我将其编辑为:

String name = node.getNodeName();
if (name.equals("Name")){...}

I part I'm stuck with is how I can parse all the endElement in SAX in a similar manner using DOM? 我坚持的一部分是如何使用DOM以类似的方式解析SAX中的所有endElement?

I've managed to print all the text using: 我设法使用以下命令打印了所有文本:

 case Node.TEXT_NODE:

    System.out.print(node.getNodeValue().trim());

but how would I go about printing something after a specific text node? 但是我将如何在特定文本节点之后打印一些内容? In SAX this could be easily done in endElement. 在SAX中,可以在endElement中轻松完成此操作。

I've been trying to use this to print the text from inside the name elements but it isn't working. 我一直试图使用它来从name元素内部打印文本,但是它不起作用。

case Node.TEXT_NODE:

if (node.getNodeName().equals("name")) {
      System.out.print(node.getNodeValue().trim());
    }

Thanks for any help on this. 感谢您对此的任何帮助。

Have you tried a recursive approach? 您是否尝试过递归方法?

public void visitNode(Node node) {
   // Print something emulating a start tag

   // Visit all child nodes recursively
   for (int i = 0; i < node.getChildNodes().getLength(); i++)
     visitNode(node.getChildeNodes().item(i));

   // Print something emulating an end tag
}

Another approach if you know the nodes that you want is to use xpath . 如果知道所需的节点,则另一种方法是使用xpath The setup can look overwhelming, but its quite powerful. 该设置可能看起来不堪重负,但功能非常强大。

String xpath="//name";
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile(xpathStatement);
NodeList nodes = (NodeList) expr.evaluate(source, XPathConstants.NODESET);
for (int i=0; i<nodes.getLength(); i++) {
    Node node = nodes.item(i);
    System.out.println(node.getNodeValue());
}

Have a look into Node javadoc - it has a fixed name "#text" ; 看一下Node javadoc-它有一个固定的名称"#text" you need to check the name of the parent, which is the tag: 您需要检查父级的名称,即标签:

case Node.TEXT_NODE:

if (node.getParentNode().getNodeName().equals("Name")) {
  System.out.print(node.getNodeValue().trim());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM