javaxml解析解决方案

javaxml解析解决方案,java,xml-parsing,Java,Xml Parsing,我面临一个问题。解析xml文件后,输出实际上不是我想要的。 为了解析xml文件,我编写了如下代码: public static void main(String argv[]) throws IOException { int suiteid = 0; String scmoid = null; int getsuiteid = -34343; FileWriter fstream = new FileWriter("G:/filewriter.txt",false); BufferedWri

我面临一个问题。解析xml文件后,输出实际上不是我想要的。 为了解析xml文件,我编写了如下代码:

public static void main(String argv[]) throws IOException {
int suiteid = 0;
String scmoid = null;
int getsuiteid = -34343;

FileWriter fstream = new FileWriter("G:/filewriter.txt",false);
BufferedWriter out = new BufferedWriter(fstream);     

try {

File fXmlFile = new File("NewFile.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);        

doc.getDocumentElement().normalize();   

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

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

    Node nNode = nList.item(temp);


    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

        Element eElement = (Element) nNode;         
        String testcasename = eElement.getAttribute("name");


        //testcase name of test suite.


        NodeList tcname = doc.getElementsByTagName("include");

        for(int temp2 =0; temp2 < tcname.getLength();temp2++){

        Node nNode1 = tcname.item(temp2);               

        if (nNode1.getNodeType() == Node.ELEMENT_NODE) {

            Element eElement1 = (Element) nNode1;

            String testcasename1 = eElement1.getAttribute("name");
            out.write( testcasename);
            out.write( "_");
            out.write( testcasename1);
            out.newLine();

        }               
      }

    }
}
} catch (Exception e) {
e.printStackTrace();
}finally {
    if (out != null) {
        out.close();
     }
  }
}
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="test" verbose="1" preserve-order="true">
<test name="testone_7654">
    <classes>
        <class name="test.Signin_0001_Test">
            <methods>               
                <include name="test_2610_Oneaddone" />
                <include name="test_2611_Oneaddtwo" />                  
                <include name="test_1677_Oneaddthree" />                    
            </methods>
        </class>
    </classes>
</test>

<test name="testtwo_8764">
    <classes>
        <class name="com.scrollmotion.workcloud.Signin_0001_Test">
            <methods>               
                <include name="test_2810_TwoOne" />
                <include name="test_2181_TwoTwo" />                 
                <include name="test_1877_TwoThree" />                   
            </methods>
        </class>
    </classes>
</test>

</suite>
testone_7654_test_2610_Oneaddone
testone_7654_test_2611_Oneaddtwo
testone_7654_test_1677_Oneaddthree
testtwo_8764_test_2810_TwoOne
testtwo_8764_test_2181_TwoTwo
testtwo_8764_test_1877_TwoThree
我希望输出如下:

public static void main(String argv[]) throws IOException {
int suiteid = 0;
String scmoid = null;
int getsuiteid = -34343;

FileWriter fstream = new FileWriter("G:/filewriter.txt",false);
BufferedWriter out = new BufferedWriter(fstream);     

try {

File fXmlFile = new File("NewFile.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);        

doc.getDocumentElement().normalize();   

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

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

    Node nNode = nList.item(temp);


    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

        Element eElement = (Element) nNode;         
        String testcasename = eElement.getAttribute("name");


        //testcase name of test suite.


        NodeList tcname = doc.getElementsByTagName("include");

        for(int temp2 =0; temp2 < tcname.getLength();temp2++){

        Node nNode1 = tcname.item(temp2);               

        if (nNode1.getNodeType() == Node.ELEMENT_NODE) {

            Element eElement1 = (Element) nNode1;

            String testcasename1 = eElement1.getAttribute("name");
            out.write( testcasename);
            out.write( "_");
            out.write( testcasename1);
            out.newLine();

        }               
      }

    }
}
} catch (Exception e) {
e.printStackTrace();
}finally {
    if (out != null) {
        out.close();
     }
  }
}
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="test" verbose="1" preserve-order="true">
<test name="testone_7654">
    <classes>
        <class name="test.Signin_0001_Test">
            <methods>               
                <include name="test_2610_Oneaddone" />
                <include name="test_2611_Oneaddtwo" />                  
                <include name="test_1677_Oneaddthree" />                    
            </methods>
        </class>
    </classes>
</test>

<test name="testtwo_8764">
    <classes>
        <class name="com.scrollmotion.workcloud.Signin_0001_Test">
            <methods>               
                <include name="test_2810_TwoOne" />
                <include name="test_2181_TwoTwo" />                 
                <include name="test_1877_TwoThree" />                   
            </methods>
        </class>
    </classes>
</test>

</suite>
testone_7654_test_2610_Oneaddone
testone_7654_test_2611_Oneaddtwo
testone_7654_test_1677_Oneaddthree
testtwo_8764_test_2810_TwoOne
testtwo_8764_test_2181_TwoTwo
testtwo_8764_test_1877_TwoThree
我必须更改代码的地方???

更改:

NodeList tcname = doc.getElementsByTagName("include");


您有两个嵌套的for循环。在外部循环中,您将遍历所有元素
test
,其中有两个元素。在内部循环中,您将遍历所有元素
包括
,其中有六个元素。但在这两种情况下,您都从同一根文档节点检索节点列表。因此,您迭代两个测试节点,并在每次迭代中处理文档中的所有包含

您可以做两件不同的事情:

  • 您不需要外部循环,只需检索
    include
    NodeList
    ,并对其进行迭代即可
  • 在内部循环中,从当前的
    test
    节点检索
    NodeList
  • 根据您的意图,一种或另一种方法可能会更好