简体   繁体   中英

Xpath in java for array of values

I have the following problem. Im using Xpath to extract the value from XML file using JAVA program

<place> 
<native name= "AAAAAA" />
<native name= "BBBBBB" />
<native name= "CCCCCC" />
<native name= "DDDDDD" />
<native name= "EEEEEE" />
</place>

Above is my partial XML file. Im using the following Xpath

/root/place/native/@name
/root/home
/root/xxxxxx

and i want my result to be like this

AAAAAA|BBBBBBB|CCCCCCC|DDDDDDD|EEEEEEEˆevergreenˆvaluesˆexample

How can i do this . Can any one help me

for (String temp : XpathValue) {
              TempFlat = xPath1.compile(temp).evaluate(xmlDocument);
                        TempFlat1 = TempFlat.replaceAll("\\s+", " ");
                        value1.append(TempFlat1);
                        value1.append((char)"ˆ");

XPathExpression#evalute will return a NodeList if you use XPathConstants.NODESET .

Once you have the NodeList , you will need to iterate the list and populate your array...

NodeList nodeList = (NodeList)xPath1.compile("/place/native[@name]").evaluate(xmlDocument, XPathConstants.NODESET);
String[] results = new String[nodeList.getLength()];
for (int index = 0; index < nodeList.getLength(); index++) {
    Node node = nodeList.item(index);
    String name = node.getAttributes().getNamedItem("name");
    results[index] = name;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM