List of usage examples for javax.xml.parsers DocumentBuilderFactory setNamespaceAware
public void setNamespaceAware(boolean awareness)
From source file:com.synclio.hawk.TutorialRouteBuilder.java
private static DocumentBuilderFactory createDocumentBuilderFactory() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setIgnoringElementContentWhitespace(true); factory.setIgnoringComments(true);// w w w. ja va 2 s. c om return factory; }
From source file:Main.java
public static String setValueXPath(String srcXmlString, String xPath, String newVal) { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); // never forget this! int i, j;/*from w ww . j a v a2s . c om*/ Document doc = null; DocumentBuilder builder = null; try { builder = domFactory.newDocumentBuilder(); doc = builder.parse(new ByteArrayInputStream(srcXmlString.getBytes())); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(xPath); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList xPathNodes = (NodeList) result; logger.debug("xpath result count: " + xPathNodes.getLength()); logger.debug(xPathNodes.item(0).getNodeName() + " = " + xPathNodes.item(0).getTextContent()); // get list of all nodes in doc NodeList nodes = doc.getElementsByTagName("*"); // iterate through all the nodes for (i = 0; i < xPathNodes.getLength(); i++) { // for each node in xpath result - traverse through all nodes in // doc to find match for (j = 0; j < nodes.getLength(); j++) { if (nodes.item(j).isSameNode(xPathNodes.item(i))) { logger.debug("Old value " + i + ": " + xPathNodes.item(i).getNodeName() + " = " + xPathNodes.item(i).getTextContent()); nodes.item(j).setTextContent(newVal); logger.debug("New value " + i + ": " + xPathNodes.item(i).getNodeName() + " = " + xPathNodes.item(i).getTextContent()); break; } } } } catch (Exception ex) { logger.error(ex.getMessage()); // ex.printStackTrace(); } return getW3CXmlFromDoc(doc); }
From source file:Main.java
public static Document getDomDocument(String source, boolean sourceIsFile) { try {//from w w w.j a v a 2 s .c om DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); // NEVER FORGET THIS DocumentBuilder builder = domFactory.newDocumentBuilder(); if (sourceIsFile == false) { return builder.parse(new InputSource(new StringReader(source))); } else { return builder.parse(source); } } catch (IOException e) { e.printStackTrace(); return null; } catch (ParserConfigurationException e) { e.printStackTrace(); return null; } catch (SAXException e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * Get xml document, by stream/* w ww . j ava2 s. c o m*/ * * @param input * @return * @throws Exception */ public static Document getDocument(InputStream input) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(input); return doc; }
From source file:Main.java
/** * Get xml document, by xml string/*from w w w . j ava2 s .c o m*/ * * @param xml * @return * @throws Exception */ public static Document getDocument(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes())); return doc; }
From source file:Main.java
public static Document stringToDocument(String xml) { Document doc = null;//from www .j av a 2s . c om DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // Turn on validation, and turn off namespaces factory.setValidating(false); factory.setNamespaceAware(false); try { DocumentBuilder builder = factory.newDocumentBuilder(); doc = builder.parse(new InputSource(new StringReader(xml))); } catch (Exception e) { e.printStackTrace(); } return doc; }
From source file:com.wso2telco.identity.application.authentication.endpoint.util.ReadMobileConnectConfig.java
public static Map<String, String> query(String XpathExpression) { Map<String, String> ConfigfileAttributes = new Hashtable<String, String>(); // standard for reading an XML file DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder;//from www.j ava 2 s . co m Document doc = null; XPathExpression expr = null; try { builder = factory.newDocumentBuilder(); doc = builder.parse(CarbonUtils.getCarbonConfigDirPath() + File.separator + "mobile-connect.xml"); // create an XPathFactory XPathFactory xFactory = XPathFactory.newInstance(); // create an XPath object XPath xpath = xFactory.newXPath(); // compile the XPath expression expr = xpath.compile("//" + XpathExpression + "/*"); // run the query and get a nodeset Object result = expr.evaluate(doc, XPathConstants.NODESET); // // cast the result to a DOM NodeList NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { ConfigfileAttributes.put(nodes.item(i).getNodeName(), nodes.item(i).getTextContent()); } } catch (SAXException e) { log.error(e.getMessage()); } catch (IOException e) { log.error(e.getMessage()); } catch (ParserConfigurationException e) { log.error(e.getMessage()); } catch (XPathExpressionException e) { log.error(e.getMessage()); } return ConfigfileAttributes; }
From source file:Main.java
/** * Creates a new {@link DocumentBuilder} object. *//*from w w w .j a v a 2s . c o m*/ private static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true); factory.setNamespaceAware(true); factory.setValidating(false); factory.setIgnoringComments(true); factory.setExpandEntityReferences(false); factory.setCoalescing(false); factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); return factory.newDocumentBuilder(); }
From source file:Main.java
public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // ist das langsammer: factory.setValidating(true); factory.setValidating(false);/*from w w w . ja v a 2 s .c o m*/ factory.setNamespaceAware(false); DocumentBuilder builder = factory.newDocumentBuilder(); return builder; }
From source file:net.mindengine.oculus.frontend.domain.document.testcase.Testcase.java
public static Testcase parse(String xmlData) throws Exception { Testcase testcase = new Testcase(); try {/* w w w .j a v a 2 s. c om*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder builder = dbf.newDocumentBuilder(); Reader reader = new CharArrayReader(xmlData.toCharArray()); org.w3c.dom.Document doc = builder.parse(new org.xml.sax.InputSource(reader)); Node root = doc.getDocumentElement(); NodeList nodeList = root.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if ("steps".equals(node.getNodeName())) { NodeList stepsList = node.getChildNodes(); for (int j = 0; j < stepsList.getLength(); j++) { Node n = stepsList.item(j); TestcaseStep step = new TestcaseStep(); step.setAction(XmlUtils.getChildNodeText(n, "action")); step.setExpected(XmlUtils.getChildNodeText(n, "expected")); step.setComment(XmlUtils.getChildNodeText(n, "comment")); testcase.steps.add(step); } } } } catch (Exception e) { } return testcase; }