-1

I have an xml file like given below

 <Placemark>
    <name>west am</name>
    <styleUrl>#msn_ylw-pushpin0</styleUrl>
    <Polygon>
        <tessellate>1</tessellate>
        <outerBoundaryIs>
            <LinearRing>
                <coordinates>
                    -59.6227959506966,55.37494940456102,0            
                </coordinates>
            </LinearRing>
        </outerBoundaryIs>
    </Polygon>
</Placemark>
<Placemark>
    <name>east am</name>
    <styleUrl>#msn_ylw-pushpin10</styleUrl>
    <Polygon>
        <tessellate>1</tessellate>
        <outerBoundaryIs>
            <LinearRing>
                <coordinates>
                    -118.5321721528021,34.65735513563493,0                   
                </coordinates>
            </LinearRing>
        </outerBoundaryIs>`
    </Polygon>
</Placemark>

How can i retrieve the tag whose tag value is "east am" using xpath.

Thanks for the support.

lal kiran
  • 69
  • 8
  • 1
    Possible duplicate of [XPath: How to select elements based on their value?](https://stackoverflow.com/questions/1198253/xpath-how-to-select-elements-based-on-their-value) – Andersson Jul 13 '18 at 09:06
  • This is kinda basics of XPath. Did you try anything? Share your XPath and describe your problem – Andersson Jul 13 '18 at 09:08

1 Answers1

0

While you need code to read and parse the xml from some source and classes to manage and manipulate the xml info set, the answer boils down to the following XPath expression:

//Placemark/name[./text()='east am']

This expression selects ...

  • name elements, being ...
  • ... children of Placemark elements ...
  • ... anywhere in the document, ...
  • ... the textual content of the name element being east am.

Alternative

//Placemark/name[string(.)='east am']

See this SO answer for a full discussion of the difference between string(.) and ./text().

Basically, on one hand, ./text() fails if the textual content of a xml node is represented with multiple text nodes. AFAIK this decision is made by the parser and you have no way to guide this choice.

On the other hand, if the targeted name node is of a complex type, ie. may have child elements with their own textual content, you would need to specify the concatenation of all of the text fragments from the target element itself and its descendants.

Code

This code is a standalone sample to gather a representation of a node referenced by a given xpath expression from the xml file given as a sample.

This code originates with this SO answer, all credits are due to its author, none to me.

Note that your xml excerpt is not a valid standalone xml file since it lacks a single root element. Add any synthetic root to the file if it does not yet have one (the xpath expression will work either way).

File names as is are xpath.java and test.51320827.xml. Tested with java 1.8.0_45;

//  https://stackoverflow.com/questions/51320827/how-can-i-retrieve-the-child-node-using-xpath-java
//
//  code taken from:    https://stackoverflow.com/a/47280397
//
//
import java.lang.*;
import java.io.*;
import javax.xml.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class xpath {
    public static void main(String[] args) {
        String xpathExpression = "//Placemark/name[./text()='east am']";

        // Read file
        Document doc = null;
        try {

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setNamespaceAware(true);
            factory.setIgnoringComments(true);
            factory.setIgnoringElementContentWhitespace(true);

            DocumentBuilder builder = factory.newDocumentBuilder();
            File file = new File( "test.51320827.xml" );
            FileInputStream stream = new FileInputStream( file );
            doc = builder.parse( stream );
        } catch (SAXException | IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }

        // get from XPath
        try {
            XPathFactory xpf = XPathFactory.newInstance();
            XPath xpath = xpf.newXPath();

            XPathExpression compile = xpath.compile(xpathExpression);
            NodeList nodeList = (NodeList) compile.evaluate(doc, XPathConstants.NODESET);

            displayNodeList(nodeList);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }

        System.out.println("\n===== ***** =====");
    }

    static void displayNodeList( NodeList nodeList ) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            String NodeName = node.getNodeName();

            NodeList childNodes = node.getChildNodes();
            if ( childNodes.getLength() > 1 ) {
                for (int j = 0; j < childNodes.getLength(); j++) {

                    Node child = childNodes.item(j);
                    short nodeType = child.getNodeType();
                    if ( nodeType == 1 ) {
                        System.out.format( "\n\t Node Name:[%s], Text[%s] ", child.getNodeName(), child.getTextContent() );
                    }
                }
            } else {
                System.out.format( "\n Node Name:[%s], Text[%s] ", NodeName, node.getTextContent() );
            }

        }
    }
}
collapsar
  • 17,010
  • 4
  • 35
  • 61