/** * Validate the given stream and return a valid DOM document for parsing. */ protected Document buildDocument(ErrorHandler handler, InputStream stream) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder parser = dbf.newDocumentBuilder(); parser.setErrorHandler(handler); return parser.parse(stream); }
private Document parseXmlString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader(xml)); return documentBuilder.parse(inputSource); }
protected final Document parse(String value) throws IllegalArgumentException { try { return _parserFactory.newDocumentBuilder().parse(new InputSource(new StringReader(value))); } catch (Exception e) { throw new IllegalArgumentException("Failed to parse JSON String as XML: "+e.getMessage(), e); } }
static String extractEncryptPart(String xml) { try { DocumentBuilder db = builderLocal.get(); Document document = db.parse(new InputSource(new StringReader(xml))); Element root = document.getDocumentElement(); return root.getElementsByTagName("Encrypt").item(0).getTextContent(); } catch (Exception e) { throw new RuntimeException(e); } }
public static Document loadXml(File xmlFile) { try { DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); doc.getDocumentElement().normalize(); return doc; } catch (ParserConfigurationException | IOException | SAXException e) { throw new RuntimeException(e); } }
private Document loadXML(String xml) { Document doc; try (InputStream xmlStream = ManifestAttributes.class.getResourceAsStream(xml)) { if (xmlStream == null) { throw new JadxRuntimeException(xml + " not found in classpath"); } DocumentBuilder dBuilder = XmlSecurity.getSecureDbf().newDocumentBuilder(); doc = dBuilder.parse(xmlStream); } catch (Exception e) { throw new JadxRuntimeException("Xml load error, file: " + xml, e); } return doc; }
public static Document loadXMLFromString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is); }
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); factory.setIgnoringElementContentWhitespace(true); try { DocumentBuilder builder = factory.newDocumentBuilder(); File file = new File("test.xml"); Document doc = builder.parse(file); // Do something with the document here. } catch (ParserConfigurationException e) { } catch (SAXException e) { } catch (IOException e) { }
/** * Parse the supplied XML stream data to a {@link Document}. * <p> * This function does not close the stream. * * @param stream The XML stream. * @return The XML {@link Document}. * @throws SAXException Error parsing the XML stream data e.g. badly formed XML. * @throws IOException Error reading from the steam. * @since 2.0 */ public static @Nonnull Document parse(@Nonnull Reader stream) throws SAXException, IOException { DocumentBuilder docBuilder; try { docBuilder = newDocumentBuilderFactory().newDocumentBuilder(); docBuilder.setEntityResolver(RestrictiveEntityResolver.INSTANCE); } catch (ParserConfigurationException e) { throw new IllegalStateException("Unexpected error creating DocumentBuilder.", e); } return docBuilder.parse(new InputSource(stream)); }
/** * Parse the given XML content to a {@link Document}. * @param xml the content to parse * @param encoding optional content encoding, if provided as metadata (e.g. in HTTP headers) * @return the parsed document */ protected Document parseXmlByteArray(byte[] xml, @Nullable String encoding) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(this.hasNamespaces); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(new ByteArrayInputStream(xml)); if (StringUtils.hasText(encoding)) { inputSource.setEncoding(encoding); } return documentBuilder.parse(inputSource); }
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import java.io.ByteArrayInputStream; public Document loadXMLFromString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(new ByteArrayInputStream(xml.getBytes())); }
public static Element parseElement(String xml) { try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbFactory.newDocumentBuilder(); Document doc = docBuilder.parse(new ByteArrayInputStream(xml.getBytes())); return doc.getDocumentElement(); } catch (Exception e) { throw new RuntimeException(e); } }
private Document readXMLResponse(InputStream stream) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document; try { document = factory.newDocumentBuilder().parse(stream); parse(document); } catch (SAXException | IOException | ParserConfigurationException e) { logger.error(e.getMessage(), e); throw new RuntimeException(e); } return document; }
//obtain Document somehow, doesn't matter how DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); org.w3c.dom.Document doc = b.parse(new FileInputStream("page.html")); //Evaluate XPath against Document itself XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList)xPath.evaluate("/html/body/p/div[3]/a", doc.getDocumentElement(), XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); ++i) { Element e = (Element) nodes.item(i); }
private org.w3c.dom.Document getDomDocument() throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); org.w3c.dom.Document document = builder.parse(getProductDataResource().getInputStream()); return document; }
public static Document readDocumentFromString(String s) throws Exception { InputSource in = new InputSource(new StringReader(s)); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(false); return factory.newDocumentBuilder().parse(in); }
private DOMSource readDOMSource(InputStream body, HttpInputMessage inputMessage) throws IOException { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setFeature( documentBuilderFactory.setFeature( "http://xml.org/sax/features/external-general-entities", isProcessExternalEntities()); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); if (!isProcessExternalEntities()) { documentBuilder.setEntityResolver(NO_OP_ENTITY_RESOLVER); Document document = documentBuilder.parse(body); return new DOMSource(document);
private static Document toXmlDocument(String xmlString) { try { InputSource xmlInputSource = new InputSource(new StringReader(xmlString)); DocumentBuilder xmlDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return xmlDocumentBuilder.parse(xmlInputSource); } catch (Exception e) { throw new RuntimeException(FORMAT_ERROR, e); } }
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException, NoSuchMethodException, InvocationTargetException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); // never forget this! DocumentBuilder builder = factory.newDocumentBuilder(); FileInputStream inputStream = new FileInputStream(new File(System.getProperty("user.home") + "/java/testng/src/test/resources/testng-all.xml")); Document doc = builder.parse(inputStream); XmlSuite result = (XmlSuite) new XDom(new TestNGTagFactory(), doc).parse(); test(result); System.out.println(result.toXml()); }
String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><a><b></b><c></c></a>"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); Document document = builder.parse( new InputSource( new StringReader( xmlString ) ) ); } catch (Exception e) { e.printStackTrace(); }