0

I'm trying to get definitions for a specific word which match to user's input string from an xml:

<?xml version="1.0" encoding="UTF-8" ?>
<diction>
<article>
<key>a</key>
<definition>
    ένας, μια, μία, ένα
</definition>
</article>
<article>
<key>a</key>
<definition>
α (στερητικό)
</definition>
</article>
<article>
<key>all</key>
<definition>
όλος, όλη, όλο
</definition>
</article>
<article>
<key>a.m</key>
<definition>
π.μ., πρωί
</definition>
</article>
<article>
<key>base</key>
<definition>
base
</definition>
</article>
</diction>

code:

ap.declareVariableExpr("myexpr", xmlInput.getText().toString().replaceAll("\\s+$", ""));

ap.selectXPath("/diction/article/key[local-name()=string($myexpr)]");
//ap.evalXPathToString();
//ap.selectXPath("/diction/article/key");
ap.bind(vn);

int i;
while ((i=ap.evalXPath()) !=-1){
    i=vn.getText();
    vn.push();
    if(vn.toElement(VTDNav.NEXT_SIBLING, "definition")) {
        int j = vn.getText();
        if (j != -1)

        parsedData=parsedData+"word: " +        vn.toNormalizedString(i) + "\n"
        + "definition: " + vn.toNormalizedString(j) +"\n";
        Log.i("first", parsedData);
    }
    vn.pop();
}
ap.resetXPath();

The above code gives me all words/definitions and not only those who match with the input string. Is the xpath expression correct? What I'm doing wrong?

jh314
  • 27,144
  • 16
  • 62
  • 82
  • Can you provide us with example of user's input and how it matches to the word in xml? – MChaker May 17 '15 at 14:22
  • for e.g the user enter the word "base" in xmlInput edittext. I declare this as "myexpr" variable to use it in selectXPath, so to get only the definition for "base". I've done this with sax xml parser, but it's slow. – user2057220 May 17 '15 at 14:47

1 Answers1

0

There is a function in AutoPilot called declareVariableExpression()that allows you to declare variable expressions.

vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30
  • I'm using this function(see above), but I don't get the desired result. I've searched a lot about vtd-xml and xpath the last days but I didn't find anything for my case. There are some related tutorials but using numbers, not strings. So I imported the xml file to sqlite database and I'll try this way, which maybe it's faster from xml parsing(my file is 6MB). I haven't use xml parsing at all until now and most for this I wanted to use it. But I found difficult to manipulate vtd-xml and sax is very slow for my case(for this I searched and found vtd-xml). – user2057220 May 19 '15 at 04:31
  • what is in your xmlInput string? – vtd-xml-author May 20 '15 at 00:01
  • I looked at your code, did you enable namespace when parsing the xml file? – vtd-xml-author May 20 '15 at 00:45
  • I gave up vtd-xml because the database way gives results very fast for the case that this will be helpful for someone else in the future, no I didn't. So maybe this was the problem. – user2057220 May 20 '15 at 06:27