javaxmlvtd-xml

read vtd-xml (ximpleware) xml in java


I try to read a XML File with Ximpleware. My Problem is, that I don't find usefull examples or tutorials.

I want to read following XML:

   <title name="bliblablu">
        <program>
            <text lang="1">
                <![CDATA[English]]>
            </text>
            <text lang="2">
                <![CDATA[Francais]]>
            </text>
            <text lang="3">
                <![CDATA[Deutsch]]>
            </text>
       </program>
       <program>
            <text lang="1">
                <![CDATA[Afrikans]]>
            </text>
            <text lang="2">
                <![CDATA[Portuges]]>
            </text>
            <text lang="3">
                <![CDATA[Italiano]]>
            </text>
       </program>
   </title>

And what I want is a simple print out like below.

1: English 2: Francais 3: Deutsch 1: Afrikans 2: Portuges 3: Italiano

How can I do this? I read the following SO Answer, SO Answer. And I tried with the following Code, but it does not work.

import com.ximpleware.AutoPilot;
import com.ximpleware.NavException;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
import com.ximpleware.XPathEvalException;
import com.ximpleware.XPathParseException;
...


final VTDGen vg = new VTDGen();
vg.parseFile(file.getAbsolutePath(), false);
VTDNav vn = vg.getNav();
final AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/title/program/text");

Log.d(ap.evalXPathToString());

int i = 0, j = 0;
String languageNr = "", languageText ="";

while ((i = ap.evalXPath()) != -1) {
    j = vn.getAttrVal("lang");

    // z = vn.toNormalizedString(i);
    if (j != -1) {
        languageNr = vn.toString(j);
        AutoPilot ap2 = new AutoPilot(vn);
        ap2.resetXPath();
        ap2.selectXPath("/ticker/program/text");
        ap2.selectElement(Integer.toString(j));
        languageText = ap2.evalXPathToString();

    }
    Log.d(languageNr);
    Log.d(languageText);
}

My output is:

1: English 2: English 3: English 1: English 2: English 3: English

Thanks for every kind of help Lukas


Solution

  • Ok, I assume you know xpath reasonably well... so I don't see why you need another AutoPilot... Here is the first version.

    import com.ximpleware.*;
    public class simpleRead {
        public static void main(String s[]) throws VTDException{
            VTDGen vg = new VTDGen();
            if (!vg.parseFile("d:\\xml\\title.xml", false)){
                return;
            }
            VTDNav vn = vg.getNav();
            AutoPilot ap = new AutoPilot(vn);
            ap.selectXPath("/title/program/text/text()");
            int i=0;
            while((i=ap.evalXPath())!=-1){
                System.out.print(" "+vn.toString(i));
            }
        }
    }
    

    Here is the second version that does the same thing...

    import com.ximpleware.*;
    public class simpleRead {
        public static void main(String s[]) throws VTDException{
            VTDGen vg = new VTDGen();
            if (!vg.parseFile("d:\\xml\\title.xml", false)){
                return;
            }
            VTDNav vn = vg.getNav();
            AutoPilot ap = new AutoPilot(vn);
            ap.selectXPath("/title/program/text");
            int i=0;
            while((i=ap.evalXPath())!=-1){
                int k=vn.getText();
                if (k!=-1)
                System.out.print(" "+vn.toString(k));
            }
        }
    }