List of usage examples for org.xml.sax InputSource InputSource
public InputSource(Reader characterStream)
From source file:Main.java
public static Document parseXmlDocument(String xml, boolean namespaceAware) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder docBuilder = buildDocumentBuilder(namespaceAware); Document doc = docBuilder.parse(new InputSource(new StringReader(xml))); // normalize text representation doc.getDocumentElement().normalize(); return doc;// w w w. j a v a 2 s.co m }
From source file:Main.java
public static boolean doesXMLMatchXSD(String xmlUrl) throws SAXException, FileNotFoundException, IOException { boolean doesMatchXSD = false; SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); SAXSource sourceXSD = new SAXSource(new InputSource(new FileInputStream(new File(xsdUrl)))); Schema schema = factory.newSchema(sourceXSD); Validator validator = (Validator) schema.newValidator(); validator.validate(new StreamSource(new File(xmlUrl))); doesMatchXSD = true;//from w w w. j a v a 2s . co m return doesMatchXSD; }
From source file:Main.java
/** * Returns root tagname/* w w w .j av a 2 s .c o m*/ * * @param xml The xml document to be searched. * @return String result. */ public static String getRootTagName(String xml) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder builder = dbf.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xml))); return document.getDocumentElement().getTagName(); }
From source file:Main.java
public static Document parseXml(InputStream in) throws IOException, SAXException, ParserConfigurationException { DocumentBuilder builder = getBuilder(); InputStream bin = new BufferedInputStream(in); Document ret = builder.parse(new InputSource(bin)); return ret;// w w w . ja va 2s. c om }
From source file:Main.java
public static Document loadString(String domContent) throws Exception { javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(false);//from www. j a va 2s .co m factory.setIgnoringElementContentWhitespace(false); factory.setValidating(false); factory.setCoalescing(false); DocumentBuilder builder = factory.newDocumentBuilder(); char[] chars = new char[domContent.length()]; domContent.getChars(0, domContent.length(), chars, 0); InputSource is = new InputSource(new CharArrayReader(chars)); return (builder.parse(is)); }
From source file:Main.java
public static boolean isXMLWellFormed(String xmlUrl) throws IOException, SAXException, ParserConfigurationException { boolean isXMLValid = false; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false);//from w ww .j a v a 2 s.co m factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = (Document) builder.parse(new InputSource(xmlUrl)); isXMLValid = true; return isXMLValid; }
From source file:Main.java
public static Document createDocument(Reader reader) throws Exception { // handler.clear(); synchronized (lock) { return builder.parse(new InputSource(reader)); }/* ww w . j a va 2s .c o m*/ }
From source file:Main.java
public static String getRequestNameFull(String xml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xml))); NodeList nodeList = document.getDocumentElement().getChildNodes(); Node reqName = document.getElementsByTagName("request-name").item(0); System.out.println(reqName.getTextContent()); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); System.out.println(i + "--" + node); if (node instanceof Element) { NodeList childNodes = node.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node cNode = childNodes.item(j); System.out.println(i + "--" + j + "--" + cNode); if (cNode instanceof Element) { String content = cNode.getLastChild().getTextContent().trim(); System.out.println(i + "--" + j + "--" + content); }/*from w w w . jav a2 s .c om*/ } } } /* * Do the parsing for reqname */ return reqName.getTextContent(); }
From source file:Main.java
public static Document getDocumentFromString(String xmlString) throws Exception { DocumentBuilderFactory oDocumentBuilderFactory = DocumentBuilderFactory.newInstance(); try {//from w w w . j a v a 2 s .co m // Do not load the DTD oDocumentBuilderFactory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", Boolean.FALSE); } catch (IllegalArgumentException e) { } DocumentBuilder oDocumentBuilder = oDocumentBuilderFactory.newDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader(xmlString)); return oDocumentBuilder.parse(inputSource); }
From source file:Main.java
public static Document loadString(String paramString) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(false);//from w w w . j a va 2 s . c om factory.setIgnoringElementContentWhitespace(false); factory.setValidating(false); factory.setCoalescing(false); DocumentBuilder builder = factory.newDocumentBuilder(); char[] arrayOfChar = new char[paramString.length()]; paramString.getChars(0, paramString.length(), arrayOfChar, 0); InputSource input = new InputSource(new CharArrayReader(arrayOfChar)); return builder.parse(input); }