List of usage examples for javax.xml.namespace NamespaceContext NamespaceContext
NamespaceContext
From source file:Main.java
public static void main(String[] args) throws Exception { String source = "<p xmlns='http://www.java2s.com/nfe' versao='2.00'></p>"; XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xPath = xPathFactory.newXPath(); NamespaceContext context = new NamespaceContext() { String PREFIX = "nfe"; String URI = "http://www.java2s.com/nfe"; @Override/*from ww w . j av a 2 s. co m*/ public String getNamespaceURI(String prefix) { return (PREFIX.equals(prefix)) ? URI : XMLConstants.NULL_NS_URI; } @Override public String getPrefix(String namespaceUri) { return (URI.equals(namespaceUri)) ? PREFIX : XMLConstants.DEFAULT_NS_PREFIX; } @Override public Iterator getPrefixes(String namespaceUri) { return Collections.singletonList(this.getPrefix(namespaceUri)).iterator(); } }; xPath.setNamespaceContext(context); InputSource inputSource = new InputSource(new StringReader(source)); String versao = xPath.evaluate("//nfe:p/@versao", inputSource); System.out.println(versao.toString()); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setValidating(false);/*from ww w . ja v a2 s. co m*/ domFactory.setNamespaceAware(true); domFactory.setIgnoringComments(true); domFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document dDoc = builder.parse("C:/data.xsd"); Node rootNode = dDoc.getElementsByTagName("xs:schema").item(0); System.out.println(rootNode.getNodeName()); XPath xPath1 = XPathFactory.newInstance().newXPath(); NamespaceContext nsContext = new NamespaceContext() { @Override public String getNamespaceURI(String prefix) { return "http://www.w3.org/2001/XMLSchema"; } @Override public String getPrefix(String namespaceURI) { return "xs"; } @Override public Iterator getPrefixes(String namespaceURI) { Set s = new HashSet(); s.add("xs"); return s.iterator(); } }; xPath1.setNamespaceContext((NamespaceContext) nsContext); NodeList nList1 = (NodeList) xPath1.evaluate("//xs:schema", dDoc, XPathConstants.NODESET); System.out.println(nList1.item(0).getNodeName()); NodeList nList2 = (NodeList) xPath1.evaluate("//xs:element", rootNode, XPathConstants.NODESET); System.out.println(nList2.item(0).getNodeName()); }
From source file:Main.java
License:asdf
public static void main(String[] args) throws Exception { String xml = "<soapenv:Envelope " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " + "xmlns:ser=\"http://services.web.post.list.com\"><soapenv:Header>" + "<authInfo xsi:type=\"soap:authentication\" " + "xmlns:soap=\"http://list.com/services/SoapRequestProcessor\">" + "<username xsi:type=\"xsd:string\">asdf@g.com</username>" + "<password xsi:type=\"xsd:string\">asdf</password></authInfo></soapenv:Header></soapenv:Envelope>"; System.out.println(xml);//from w ww . j a v a 2 s .c om DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(xml))); XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(new NamespaceContext() { @Override public Iterator getPrefixes(String arg0) { return null; } @Override public String getPrefix(String arg0) { return null; } @Override public String getNamespaceURI(String arg0) { if ("soapenv".equals(arg0)) { return "http://schemas.xmlsoap.org/soap/envelope/"; } return null; } }); XPathExpression expr = xpath.compile("/soapenv:Envelope/soapenv:Header/authInfo/password"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println("Got " + nodes.getLength() + " nodes"); }
From source file:com.semsaas.jsonxml.tools.JsonXpath.java
public static void main(String[] args) { /*//w w w . j av a 2 s .co m * Process options */ LinkedList<String> files = new LinkedList<String>(); LinkedList<String> expr = new LinkedList<String>(); boolean help = false; String activeOption = null; String error = null; for (int i = 0; i < args.length && error == null && !help; i++) { if (activeOption != null) { if (activeOption.equals("-e")) { expr.push(args[i]); } else if (activeOption.equals("-h")) { help = true; } else { error = "Unknown option " + activeOption; } activeOption = null; } else { if (args[i].startsWith("-")) { activeOption = args[i]; } else { files.push(args[i]); } } } if (error != null) { System.err.println(error); showHelp(); } else if (help) { showHelp(); } else { try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); for (String f : files) { System.out.println("*** " + f + " ***"); try { // Create a JSON XML reader XMLReader reader = XMLReaderFactory.createXMLReader("com.semsaas.jsonxml.JsonXMLReader"); // Prepare a reader with the JSON file as input InputStreamReader stringReader = new InputStreamReader(new FileInputStream(f)); SAXSource saxSource = new SAXSource(reader, new InputSource(stringReader)); // Prepare a DOMResult which will hold the DOM of the xjson DOMResult domResult = new DOMResult(); // Run SAX processing through a transformer // (This could be done more simply, but we have here the opportunity to pass our xjson through // an XSLT and get a legacy XML output ;) ) transformer.transform(saxSource, domResult); Node dom = domResult.getNode(); XPathFactory xpathFactory = XPathFactory.newInstance(); for (String x : expr) { try { XPath xpath = xpathFactory.newXPath(); xpath.setNamespaceContext(new NamespaceContext() { public Iterator getPrefixes(String namespaceURI) { return null; } public String getPrefix(String namespaceURI) { return null; } public String getNamespaceURI(String prefix) { if (prefix == null) { return XJSON.XMLNS; } else if ("j".equals(prefix)) { return XJSON.XMLNS; } else { return null; } } }); NodeList nl = (NodeList) xpath.evaluate(x, dom, XPathConstants.NODESET); System.out.println("-- Found " + nl.getLength() + " nodes for xpath '" + x + "' in file '" + f + "'"); for (int i = 0; i < nl.getLength(); i++) { System.out.println(" +(" + i + ")+ "); XMLJsonGenerator handler = new XMLJsonGenerator(); StringWriter buffer = new StringWriter(); handler.setOutputWriter(buffer); SAXResult result = new SAXResult(handler); transformer.transform(new DOMSource(nl.item(i)), result); System.out.println(buffer.toString()); } } catch (XPathExpressionException e) { System.err.println("-- Error evaluating '" + x + "' on file '" + f + "'"); e.printStackTrace(); } catch (TransformerException e) { System.err.println("-- Error evaluating '" + x + "' on file '" + f + "'"); e.printStackTrace(); } } } catch (FileNotFoundException e) { System.err.println("File '" + f + "' was not found"); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } } catch (TransformerConfigurationException e) { e.printStackTrace(); } } }
From source file:Main.java
public static XPath namespaceAwareXpath(final String prefix, final String nsURI) { XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); NamespaceContext ctx = new NamespaceContext() { @Override//from ww w . ja va 2s .c o m public String getNamespaceURI(String aPrefix) { if (aPrefix.equals(prefix)) return nsURI; else return null; } @Override public Iterator getPrefixes(String val) { throw new UnsupportedOperationException(); } @Override public String getPrefix(String uri) { throw new UnsupportedOperationException(); } }; xpath.setNamespaceContext(ctx); return xpath; }
From source file:Main.java
/** * Creates a NamespaceContext object with the following list of namespaces: * /*from w w w. ja va 2s .c o m*/ * <dl> * <dt>tei</dt> * <dd>http://www.tei-c.org/ns/1.0</dd> * </dl> * * @return A NamespaceContext object */ public static NamespaceContext getTEINamespaceContext() { NamespaceContext ctx = new NamespaceContext() { public String getNamespaceURI(String prefix) { String uri; if (prefix.equals("tei")) uri = "http://www.tei-c.org/ns/1.0"; else uri = null; return uri; } // Dummy implementation - not used! public Iterator<String> getPrefixes(String val) { return null; } // Dummy implemenation - not used! public String getPrefix(String uri) { return null; } }; return ctx; }
From source file:Main.java
public static XPath getSchemaXPath() { XPath xPath = schemaXPathThreadLocal.get(); if (xPath == null) { xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext(new NamespaceContext() { @Override/* w w w . j a v a 2 s .com*/ public String getNamespaceURI(String prefix) { return XMLConstants.W3C_XML_SCHEMA_NS_URI; } @Override public String getPrefix(String namespaceURI) { return "xs"; } @Override public Iterator getPrefixes(String namespaceURI) { return Arrays.asList("xs").iterator(); } }); schemaXPathThreadLocal.set(xPath); } return xPath; }
From source file:Main.java
public static XPathExpression buildXPath(String path, Map<String, String> map) { XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); if (map != null) xpath.setNamespaceContext(new NamespaceContext() { public Iterator getPrefixes(String namespaceURI) { throw new UnsupportedOperationException(); }/*from w w w. jav a 2s .co m*/ public String getPrefix(String namespaceURI) { throw new UnsupportedOperationException(); } public String getNamespaceURI(String prefix) { Objects.requireNonNull(prefix); if (map.containsKey(prefix)) return map.get(prefix); return XMLConstants.NULL_NS_URI; } }); try { return xpath.compile(path); } catch (XPathExpressionException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static XPathExpression newXPath(String xpath, final Map<String, String> namespaces) throws XPathExpressionException { final XPath xp = newXPathFactory().newXPath(); xp.setNamespaceContext(new NamespaceContext() { @Override/*from w w w . j a v a 2 s. co m*/ public String getNamespaceURI(String prefix) { return namespaces != null ? namespaces.get(prefix) : null; } @Override public String getPrefix(String namespaceURI) { if (namespaces == null) { return null; } else { final Iterator i = getPrefixes(namespaceURI); if (i.hasNext()) { return (String) i.next(); } else { return null; } } } @Override public Iterator getPrefixes(String namespaceURI) { if (namespaces == null) { return null; } else { ArrayList<String> list = new ArrayList<String>(); for (Map.Entry<String, String> entry : namespaces.entrySet()) { if (entry.getValue().equals(namespaceURI)) { list.add(entry.getKey()); } } return list.iterator(); } } }); return xp.compile(xpath); }
From source file:Main.java
/** * @return/*from w w w . j a va2 s . c om*/ */ public static XPath getHtmlXPath() { XPathFactory factory = XPathFactory.newInstance(); XPath htmlPath = factory.newXPath(); htmlPath.setNamespaceContext(new NamespaceContext() { public String getNamespaceURI(String prefix) { return "http://www.w3.org/1999/xhtml"; } public String getPrefix(String namespaceURI) { return "h"; } public Iterator<?> getPrefixes(String namespaceURI) { List<String> prefixes = new ArrayList<String>(); prefixes.add("h"); return prefixes.iterator(); } }); return htmlPath; }