1

I am writing a java program to search for text and return its node information.

  1. The program should search for text anywhere in the document and.
  2. Return 5 predefined node and node-values.

Sample xml file : -

<project xmlns="https://example.hoom/go/bing">
    <name>purchaseOrder </name>
    <property name="included" type="hidden">true</property>
    <locales>
        <locale>en</locale>
        <locale>hi</locale>
    </locales>
    <defaultLocale>en</defaultLocale>
    <gamespace>
        <name locale="en">hambook</name>
        <name locale="de">hambook</name>
        <lastChanged>2014-03-05T18:47:30</lastChanged>
        <lastChangedBy>userx</lastChangedBy>
        <property name="included" type="hidden">true</property>
        <gamespace>
            <name locale="en">DbBook</name>
            <name locale="zw">DbBook</name>
            <hecrotSubject status="valid">
                <name locale="en">hexValue</name>
                <name locale="zw">hexValue</name>
                <hecrotItem>
                    <name locale="en">hireValue</name>
                    <name locale="zw">hireValue</name>
                    <hello>searchTerm</hello>
                </hecrotItem>
            </hecrotSubject>
        </gamespace>
    </gamespace>
    <gamespace>
        <name locale="en">Names</name>
        <lastChanged>2016-01-12T12:42:46</lastChanged>
        <gamespace>
            <name locale="en">Database Layer</name>
            <name locale="zw">Database Layer</name>
            <hecrotSubject status="valid">
                <name locale="en">qsxyz</name>
                <hecrotItem>
                    <name locale="en">myName</name>
                    <hello>...Hi there..</hello>
                </hecrotItem>
            </hecrotSubject>
        </gamespace>
    </gamespace>
</project>

My current xpath is :-

"//*[local-name()='gamespace']/*[local-name()='hecrotSubject']/*[local-name()='hecrotItem'][contains(., '"& searchTerm &"')]/ancestor-or-self::*/*[local-name()='name' and @locale='en']"

Which is giving only the root tag using xpath.compile().evaluate(). While result I need is

name value of five predefined nodes if they contain the search text (searchTerm in this sample xml).

sample result should be:-

Project - purchaseOrder

gamespace - hambook

gamespace - DbBook

hecrotSubject - hexValue

hecrotItem - hireValue

Edit

I am using following statements in java : -

String expression = Xpath;
Strings vals = xPath.compile(expression).evaluate(xmlDocument);
System.out.println(vals);
Community
  • 1
  • 1
user2816085
  • 655
  • 4
  • 19

1 Answers1

0

I do not know why you get only the root tag.
But in your xpaht the case of some values (node names) are wrong. Fixing hecrotSubject and hecrotItem and it should work.
But by the way I do not see why you are using local-name with setting up the right name-space something like the following should also do:

/b:gamespace/b:hecrotsubject/b:hecrotitem[contains(., 'searchTerm')]/ancestor-or-self::*/b:name[@locale='en']" 

Update doe to updated question

The main problem is that you need iterate over the NodeList which could be returned by xpath evaluate.

Have a look to e.g.: How to read XML using XPath in Java

Community
  • 1
  • 1
hr_117
  • 9,589
  • 1
  • 18
  • 23