Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.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
如何使用Java在XML文件中获取节点名?_Java_Xml - Fatal编程技术网

如何使用Java在XML文件中获取节点名?

如何使用Java在XML文件中获取节点名?,java,xml,Java,Xml,我想询问第二级(节点)名称,例如failure,condNotMeet。 我有一个xml文件,如下所示: <testcase classname="name1" name="description1" time="19.274"> <failure type="toBeMsg" message="error1" capture="_201601021755993.png"> </failure> </testcase> <tes

我想询问第二级(节点)名称,例如failure,condNotMeet。 我有一个xml文件,如下所示:

<testcase classname="name1" name="description1" time="19.274">
   <failure type="toBeMsg" message="error1" capture="_201601021755993.png">
   </failure>
</testcase>

<testcase classname="name2" name="description2" time="19.274">
   <failure type="toBeMsg" message="error2" capture="_20132143993.png">
   </failure>
</testcase>

<testcase classname="name3" name="description3" time="19.274">
   <condNotMeet type="toBeMsg" message="error3" capture="_20160123412.png">
   </condNotMeet>
</testcase>

但是,如何请求“testcase”-s子节点名称,例如:failure,condNotMeet

我有以下代码要启动:

try {
            File fXmlFile = new File("././full_1_tests.xml"); 
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            org.w3c.dom.Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("testcase");

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);

                //System.out.println("\nCurrent Element :" + nNode.getNodeName() + " Node.ELEMENT_NODE::"+  Node.ELEMENT_NODE);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    System.out.println("Test description: " + eElement.getAttribute("name") + " ");

                    // here it is the problem, how to get firstChildNode name???????? ...
                    System.out.println("|| Status: "+ nNode.getNodeName() ???? ); // Status: failure, condNotMee t

                }

            }

        } catch (Exception e) {
            System.out.println("Error : " + e);
        }
试试看{
File fXmlFile=新文件(“././full_1_tests.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
org.w3c.dom.Document doc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“testcase”);
对于(int-temp=0;temp
获得
testcase
元素的元素节点后:

Element eElement = (Element) nNode;
System.out.println("Test description: " + eElement.getAttribute("name") + " ");
您可以获取该元素的子元素并遍历此列表:

NodeList children = eElement.getChildNodes();
for (...
或者,您可以获得第一个子元素(如果这是您唯一感兴趣的元素):


(这应该会成功,因为您规范了文档)。

如果您想直接获取二级节点,假设您想要
condNotMeet

因此,您可以使用以下代码:

 XPath xPath = (XPath) XPathFactory.newInstance().newXPath();


 String condNotMeetExpression = "//testcase/condNotMeet";
 Node node = (Node) xPath.compile(condNotMeetExpression).evaluate(doc,
    XPathConstants.NODE);
String failure Expression = "//testcase/failure";
NodeList list= (NodeList)xPath.compile(failureExpression).evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < list.getLength(); i++) {
    Node node = list.item(i);
    //further code foe getting attributes
}
在这里,您将获得condNotMeet节点。其他节点也一样

String failure Expression = "//testcase/failure";
Node node = (Node) xPath.compile(failureExpression).evaluate(doc,
        XPathConstants.NODE);

                   OR
如果需要所有故障节点的列表,则可以使用以下代码:

 XPath xPath = (XPath) XPathFactory.newInstance().newXPath();


 String condNotMeetExpression = "//testcase/condNotMeet";
 Node node = (Node) xPath.compile(condNotMeetExpression).evaluate(doc,
    XPathConstants.NODE);
String failure Expression = "//testcase/failure";
NodeList list= (NodeList)xPath.compile(failureExpression).evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < list.getLength(); i++) {
    Node node = list.item(i);
    //further code foe getting attributes
}
String failure Expression=“//testcase/failure”;
NodeList list=(NodeList)xPath.compile(failureExpression).evaluate(doc,XPathConstants.NODESET);
对于(int i=0;i
目前这是结果:测试描述:描述1 | |状态:空。但是,我想要这个结果:测试描述:描述1 | |状态:失败测试描述:描述2 | |状态:失败测试描述:描述3 | |状态:条件满足您发布的xml格式不正确。所以我想知道,您的文档是什么…为什么没有正确发布xml,因为我忘记了复制过去: