繁体   English   中英

如何使用xpath dom java获取子节点的节点列表?

[英]how can is get the nodelist of child node using xpath dom java?

<a>
    <b>
        <c type="lol">
            <d>1</d>
            <f>2</f>
        </c>
        <c type="lol">
            <d>2</d>
            <f>2</f>
        </c>
        <c type="h">
            <d>v</d>
            <f>d</f>
        </c>
    </b>
</a>

DocumentBuilderFactory dBFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dB = dBFactory.newDocumentBuilder();
Document doc = dB.parse(url);     
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

如何获得子节点的节点列表,即我需要获得“ b”的子节点(具有3个“ c”节点的节点列表)。

您可以使用jOOX然后编写

List<Element> elements = $(doc).find("b").children().get();

或使用DOM:

// Beware, this list also contains the blank text nodes around the <c/> elements,
// if your document is formatted.
NodeList list = doc.getElementsByTagName("b").item(0).getChildNodes();

更新 :如果您想进一步遍历您的DOM文档(即,获得注释中提到的"c"的子节点,那么我真的建议jOOX:

// This will find all "c" elements, and then return all children thereof
$(doc).find("c").children();

// This will return "d", "f", "d", "f", "d", "f":
List<String> tags = $(doc).find("c").children().tags();

// This will return "1", "2", "2, "2", "v", "d":
List<String> texts = $(doc).find("c").children().texts();

对DOM进行相同的操作将变得非常冗长:

List<Element> elements = new ArrayList<Element>();
List<String> tags = new ArrayList<String>();
List<String> texts = new ArrayList<String>();

NodeList c = doc.getElementsByTagName("c");
for (int i = 0; i < c.getLength(); i++) {
  if (c.item(i) instanceof Element) {
    NodeList children = c.item(i).getChildNodes();

    for (int j = 0; j < children.getLength(); j++) {
      if (children.item(j) instanceof Element) {
        elements.add((Element) children.item(j));
        tags.add(((Element) children.item(j)).getTagName());
        texts.add(children.item(j).getTextContent());
      }
    }
  }
}

更新2(请对您将来的问题更具体...!) :使用XPath,请执行以下操作:

XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expression = xpath.compile("//c/*");
NodeList nodes = (NodeList) expression.evaluate(
  document.getDocumentElement(), XPathConstants.NODESET);

这是使用XPath的示例

String xmlSource = "<a>" +
                    "<b>" +
                        "<c type='lol'>" +
                            "<d>1</d>" +
                            "<f>2</f>" +
                        "</c>" +
                        "<c type='lol'>" +
                            "<d>2</d>" +
                            "<f>2</f>" +
                        "</c>" +
                        "<c type='h'>" +
                            "<d>v</d>" +
                            "<f>d</f>" +
                        "</c>" +
                    "</b>" +
                "</a>";

XPath xPath = XPathFactory.newInstance().newXPath(); 
String expression = "/a/b/c";   

InputSource inputSource = new InputSource(new StringReader(xmlSource));             
NodeList nodes = (NodeList) xPath.evaluate(expression, inputSource, XPathConstants.NODESET);

for(int i = 0; i < nodes.getLength(); i++) {
    System.out.println(nodes.item(i).getAttributes().getNamedItem("type").getNodeValue());    
}

您将需要导入以下内容

import java.io.StringReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

暂无
暂无

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

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