Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用xpath dom java获取子节点的节点列表?_Java_Xml_Dom_Xpath_Jxl - Fatal编程技术网

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

如何使用xpath dom java获取子节点的节点列表?,java,xml,dom,xpath,jxl,Java,Xml,Dom,Xpath,Jxl,如何获取子节点的节点列表,即我需要获取“b”的子节点(节点列表有3个“c”节点) 您可以使用 DocumentBuilderFactory dBFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dB = dBFactory.newDocumentBuilder(); Document doc = dB.parse(url); System.out.println("Root element :" + doc.

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

您可以使用

DocumentBuilderFactory dBFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dB = dBFactory.newDocumentBuilder();
Document doc = dB.parse(url);     
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
对DOM执行同样的操作将变得非常冗长:

// 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();
你可以使用它,然后再写

DocumentBuilderFactory dBFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dB = dBFactory.newDocumentBuilder();
Document doc = dB.parse(url);     
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
对DOM执行同样的操作将变得非常冗长:

// 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();

下面是一个使用XPath的示例

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

下面是一个使用XPath的示例

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

假设我想要“c”的子节点?抱歉,我有一个复杂的xml,我必须在其中获取子节点,grand child nodelist(child has child及其子nodelist)。。。代码有帮助,我可以说doc.getElementsByTagName(“c”).item(0.getchildnode();感谢您的回复,但我只需要使用xpath。。。。我的代码已经完成70%。。由于时间所剩无几,我无法使用新方法..除了XPath标记,您的问题中没有任何内容表明。。。你能不能至少相应地更新你的问题?假设我想要“c”的子节点?抱歉,我有一个复杂的xml,我必须在其中获取子节点,grand child nodelist(child has child及其子nodelist)。。。代码有帮助,我可以说doc.getElementsByTagName(“c”).item(0.getchildnode();感谢您的回复,但我只需要使用xpath。。。。我的代码已经完成70%。。由于时间所剩无几,我无法使用新方法..除了XPath标记,您的问题中没有任何内容表明。。。请你至少更新一下你的问题好吗
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expression = xpath.compile("//c/*");
NodeList nodes = (NodeList) expression.evaluate(
  document.getDocumentElement(), XPathConstants.NODESET);
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;