parsing using xpath giving blank spaces

I am trying to extract container node from the xml but the below caode is giving me blank spaces . I am using xpath in java to get the container.

 public static void main(String[] args) throws SAXException, IOException,
        ParserConfigurationException, TransformerException, XPathExpressionException {


    String xml = "<page>n" +
            "    <body>n" +
            "        <category curation_algm="algm1" >n" +
            "            <container id="1_2" >n" +
            "                <item bgcolor="#885159"/>n" +
            "                <item bgcolor="#8851593"/>n" +
            "                <item bgcolor="#8851594"/>n" +
            "                <item bgcolor="#8851595"/>n" +
            "                <item bgcolor="#8851596"/>n" +
            "                <item bgcolor="#8851598"/>n" +
            "                n" +
            "            </container>n" +
            "            </category>n" +
            "            </body>n" +
            "            </page>";

    InputSource source = new InputSource(new StringReader(xml));

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(source);

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();

    String msg = xpath.evaluate("/page/body/category/container[@id="1_2"]", document);
    System.out.println("msg=" + msg );


}

Leave a Reply

avatar
  Subscribe  
Notify of