Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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,我有以下xml <?xml version="1.0"?> <testsuites xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <testsuite tests="4" failures="3" errors="0" package="HPToolsFileSystemRunner"> <test

我有以下xml

<?xml version="1.0"?>
<testsuites xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <testsuite tests="4" failures="3" errors="0" package="HPToolsFileSystemRunner">
 <testcase name="C:\HMS\Functional\Functional_Apply_Leave" report="C:\HMS\Functional\Functional_Apply_Leave\Report" time="138.878906" classname="All-Tests.C:\HMS\Functional" status="fail">
  <failure message="Test failed" />
</testcase>
<testcase name="C:\HMS\Functional\Functional_Overtime_Request" report="C:\HMS\Functional\Functional_Overtime_Request\Report" time="143.9445962" classname="All-Tests.C:\HMS\Functional" status="fail">
  <failure message="Test failed" />
</testcase>
</testsuite>
</testsuites>

我只想使用java循环测试用例状态。如果状态通过,我想执行方法1,如果状态失败,我想使用java执行方法2,我编写了以下代码 它打印所有属性

 private static void loopNode(NodeList nodeList) {

for (int count = 0; count < nodeList.getLength(); count++) {

Node tempNode = nodeList.item(count);

// make sure it's element node.
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

    // get node name and value
    System.out.println(tempNode.getNodeName());
    System.out.println(tempNode.getTextContent());

    if (tempNode.hasAttributes()) {

        // get attributes names and values
        NamedNodeMap nodeMap = tempNode.getAttributes();

        for (int i = 0; i < nodeMap.getLength(); i++) {

            Node node = nodeMap.item(i);
            System.out.println(node.getNodeName());
            System.out.println(node.getNodeValue());

        }

    }

    if (tempNode.hasChildNodes()) {

        // loop again if has child nodes
    loopNode(tempNode.getChildNodes());

    }

    System.out.println(tempNode.getNodeName());

}

}
私有静态void loopNode(NodeList NodeList){
对于(int count=0;count
看看XStream 它将xml结构映射到适当的java对象

然后,您可以显式地提取XML表示对象的“测试用例列表”,并执行所需的逻辑。

您几乎完成了

使用此for循环可根据条件执行方法

for (int i = 0; i < nodeMap.getLength(); i++) {

    if( node.getNodeName().equals( "status" ) ){

        if( node.getNodeValue().equals( "true" ) )
           method1()
         else
           method1()
    }
}
for(int i=0;i
到目前为止,您只描述了您的需求。请添加您到目前为止编写的代码以解决您的问题;并指出您的困境。您知道,这不是免费的“我们为您工作”在其他人做你工作的地方提供服务。我们帮助你解决问题;我们不为你解决问题。添加了我的代码,但它会打印所有值。因为你编写了打印所有值的代码。基本上,你必须使用这些方法,你当前使用的方法是执行转储值…来比较这些值;然后执行不同的操作。对不起,这是很基本的东西,我有一段时间在想如何解释它。