質問

XML document:

<?xml version="1.0" encoding="utf-8"?>
<vdsDataSet>
  <date>1900-01-01</date>
  <vdsData>
    <contractId>contractId1</contractId>
    <periodNum>1</periodNum>
    ...
  </vdsData>
  <vdsData>
    <contractId>contractId2</contractId>
    <periodNum>-128</periodNum>
 ...
  </vdsData>
</vdsDataSet>

I am trying to get all the Contract IDs and all the associated period numbers. So output should look like this:

contractId1
1
contractId2
-128

Right now, I am only getting:

contractId1
1

Here is my code:

    VTDGen vg = new VTDGen();
    vg.parseFile("vdsDataSet.xml",false);
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    int result = -1;
    ap.selectXPath("/vdsDataSet/vdsData/contractId");
    while((result = ap.evalXPath())!=-1){
        int t = vn.getText(); // get the index of the text (char data or CDATA)
        String g = null;
        if (t!=-1)
        {
            g = "" + vn.toNormalizedString(t);
            System.out.println(g);
            AutoPilot ap2 = new AutoPilot(vn);
            ap2.selectXPath("/vdsDataSet/vdsData/periodNum[../contractId=\""+g+"\"]");
            int result2= -1;
            while((result2 = ap2.evalXPath())!=-1){
                int t1 = vn.getText(); // get the index of the text (char data or CDATA)
                if (t1!=-1)
                    System.out.println(""+vn.toNormalizedString(t1));

            }

        }   
    }

Does anyone know how I can fix this so that it shows ALL Contract ID and the associated period numbers?

If there's anyone who could help me out, your help would be greatly appreciated. Thanks in advance!

Edit: Marcus has asked me to add the version with looping through vdsData first. Here is my code:

    ap.selectXPath("/vdsDataSet/vdsData");
    while((result = ap.evalXPath())!=-1){
AutoPilot ap2 = new AutoPilot(vn);
             ap2.selectXPath("/vdsDataSet/vdsData/contractId");
             int result2= -1;
             while((result2 = ap2.evalXPath())!=-1){
                 int t1 = vn.getText(); // get the index of the text (char data or CDATA)
                 if (t1!=-1)
                  System.out.println(""+vn.toNormalizedString(t1));
         }
             ap2.selectXPath("/vdsDataSet/vdsData/periodNum");
             int result21= -1;
             while((result21 = ap2.evalXPath())!=-1){
                 int t1 = vn.getText(); // get the index of the text (char data or CDATA)
                 if (t1!=-1)
                  System.out.println(""+vn.toNormalizedString(t1));

     }

Unfortunately this only gives the following:

contractId1
contractId2
1
-128

rather than:

 contractId1
 1
 contractId2
 -128

Added a slightly modified version of vtd-xml-author's solution (to get it to print the output and to retrieve the correct child element for periodnum):

    ap.selectXPath("/vdsDataSet/vdsData");
    while((result = ap.evalXPath())!=-1){
        vn.push();
        vn.toElement(VTDNav.FC,"contractId");
        int i=-1;
        i= vn.getText(); //value for contractID here
        System.out.println(vn.toNormalizedString(i)); //added in printlns
        //vn.toElement(VTDNav.P);
        vn.toElement(VTDNav.NS, "periodNum"); //added in "periodNum" after VTDNav.NS
        i=vn.getText(); 
        i= vn.getText(); // value for periodNum here
        System.out.println(vn.toNormalizedString(i));
        vn.pop();
    }

This code actually works and does what I want it to...Assuming there is one element of periodNum...but what do I do if I have multiple periodNums?

役に立ちましたか?

解決

Below is one way of doing it. There are other ways that you might be able to figure out later. Let me know if it works for you or not.

ap.selectXPath("/vdsDataSet/vdsData");
while((result = ap.evalXPath())!=-1){
    vn.push();
    vn.toElement(VTDNav.FC,"contractID");
    int i=-1;
    i= vn.getText(); //value for contractID here
    //vn.toElement(VTDNav.P);
    vn.toElement(VTDNav.NS);
    i=vn.getText(); 
    i= vn.getText(); // value for periodNum here
    vn.pop();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top