NamespaceContext context = new NamespaceContextMap("http://www.abc.com/" );
NamespaceContext ctx = new ItunesNamespaceContext(); XPathFactory xpathFact = XPathFactory.newInstance(); XPath xpath = xpathFact.newXPath(); xpath.setNamespaceContext(ctx); String IMAGE_XPATH = "//channel/itunes:image/@href"; String imageUrl = path.compile(IMAGE_XPATH).evaluate(doc,XPathConstants.STRING).toString();
NamespaceContext context = new NamespaceContextMap( "soapenv", "http://schemas.xmlsoap.org/soap/envelope/", "wsa", "http://www.w3.org/2005/08/addressing", "axis2", "http://ws.apache.org/namespaces/axis2"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); xpath.setNamespaceContext(context); XPathExpression expression = xpath.compile("/soapenv:Envelope/soapenv:Header/wsa:ReplyTo/wsa:ReferenceParameters/axis2:ServiceGroupId/text()"); System.out.println(expression.evaluate(myXml));
String xml = "<double xmlns='http://www.somewebsite.com/'>2.0</double>"; Reader reader = new StringReader(xml); XPath xpath = XPathFactory.newInstance() .newXPath(); NamespaceContext context = new NamespaceContextMap("foo", "http://www.somewebsite.com/"); xpath.setNamespaceContext(context); String value = xpath.evaluate("/foo:double", new InputSource(reader)); System.out.println(value);
public static void main(String[] args) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); NamespaceContext namespaceContext = new XmlNamespaceResolver(); String xPath = "/CATALOG/MAIN/CD"; InputStream in = MainApp.class.getClassLoader().getResourceAsStream("test.xml"); XmlNodeInfo rootNode = new XmlNodeInfo(XmlNodeInfo.ROOT_NODE_NAME); CustomSAXHandler customSAXHandler = new CustomSAXHandler(xPath, rootNode, namespaceContext); saxParser.parse(in, customSAXHandler); List<String> results = customSAXHandler.getResults(); // result strings }
NamespaceContext context = new NamespaceContextMap( "html", "http://www.w3.org/TR/REC-html40", "xsl", "http://www.w3.org/1999/XSL/Transform",