Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

errors when I parse pom.xml with xpath

843834Jul 9 2010 — edited Jul 15 2010
This code works when i parse normal xml file but when i parse pom.xml it is not really parsing to get information can you tell me why??????????
This fetches data from normal xml file but unable to do same on pom.xml

you can check that this code works it will get name who's id is owner in developer tag.
=====================
code
=========================
package test;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XPathExample {

private static String opName;
public static void main(String[] args)
throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException {
try
{
File pFile = new File("file.xml");
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(pFile);

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//developer[id='owner']/name/text()");

Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nList=(NodeList) result;
int numOfNodes = nList.getLength();
System.out.println("# of nodes: " + nList.getLength());
if(numOfNodes==0){
opName="No_owner";
System.out.println(opName);
}
else{
for (int i=0; i<numOfNodes; i++)
{
opName=nList.item(i).getNodeValue();
System.out.print(opName);
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}

Edited by: kanty on Jul 9, 2010 12:14 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 12 2010
Added on Jul 9 2010
9 comments
473 views