List of usage examples for org.dom4j.io SAXReader SAXReader
public SAXReader(String xmlReaderClassName) throws SAXException
From source file:com.alibaba.citrus.springext.support.SchemaUtil.java
License:Open Source License
/** ?dom */ public static Document readDocument(InputStream istream, String systemId, boolean close) throws DocumentException { SAXReader reader = new SAXReader(false); Document doc = null;//w ww .j a v a2 s . c o m try { doc = reader.read(istream, systemId); } finally { if (close && istream != null) { try { istream.close(); } catch (Exception e) { // ignored } } } return doc; }
From source file:com.alibaba.stonelab.toolkit.learning.xml.Dom4jParser.java
License:Open Source License
public static void dom4j() throws Exception { SAXReader reader = new SAXReader(true); reader.setEntityResolver(new EntityResolver()); reader.setFeature("http://xml.org/sax/features/validation", true); reader.setFeature("http://apache.org/xml/features/validation/schema", true); Document doc = reader.read(Dom4jParser.class.getResourceAsStream(XML_LOCATION)); System.out.println(doc);//from w ww. ja va2 s. c om }
From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java
License:Open Source License
/** * Create dom4j document from xmlSource//from w w w . ja v a 2s . c o m * * @param xmlSource * URL?XML?XML?? Object * @param validate * boolean * @param encoding * String * @throws DocumentException * @throws IOException * @return Document * @throws BaseException */ public static Document createDocument(Object xmlSource, boolean validate, String encoding) throws BaseException { // Use xerces and validate XML file if (xmlSource instanceof Document) return (Document) xmlSource; Document doc = null; SAXReader saxReader = new SAXReader(true); saxReader.setValidation(validate); if (encoding == null || encoding.equals("")) { encoding = DEFAULT_ENCODING; } // Check input source type if (xmlSource instanceof StringBuffer) xmlSource = ((StringBuffer) xmlSource).toString(); try { if (xmlSource instanceof String) { if (((String) xmlSource).startsWith("<")) { // Treat the String as XML code StringReader reader = new StringReader(xmlSource.toString()); DocumentHelper.parseText(xmlSource.toString()); doc = saxReader.read(reader, encoding); } else { doc = saxReader.read(new File((String) xmlSource)); } } else if (xmlSource instanceof File) { doc = saxReader.read((File) xmlSource); } else if (xmlSource instanceof InputStream) { doc = saxReader.read((InputStream) xmlSource); } else if (xmlSource instanceof Reader) { doc = saxReader.read((Reader) xmlSource); } else if (xmlSource instanceof URL) { doc = saxReader.read((URL) xmlSource); } } catch (Exception ex) { ex.printStackTrace(); throw new BaseException("UTIL-0001", ex); } return doc; }
From source file:com.cladonia.xml.HTMLUtilities.java
License:Open Source License
/** * Cleans up HTML, makes HTML well formed etc * * @param text The HTML//from w w w. j ava 2 s.c o m * @param encoding The encoding */ public static String cleanUpHTML(String text) throws UnsupportedEncodingException, IOException, SAXNotRecognizedException, SAXNotSupportedException, DocumentException { // set the paser to use Neko HTML configuration SAXParser parser = new SAXParser(new HTMLConfiguration()); parser.setFeature(NOTIFY_CHAR_REFS, true); parser.setFeature(NOTIFY_HTML_BUILTIN_REFS, true); parser.setFeature(IGNORE_SPECIFIED_CHARSET, true); // set the parser to use lower case parser.setProperty(ELEMENT_CASE, "lower"); parser.setProperty(ATTR_CASE, "lower"); parser.setProperty(ENCODING_PROPERTY, "UTF-8"); // create a dom4j SaxReader SAXReader reader = new SAXReader(parser); // get the bytes from the input text ByteArrayInputStream stream = new ByteArrayInputStream(text.getBytes("UTF-8")); // using sax read the stream into a dom4j dom XDocument doc = (XDocument) reader.read(stream); // write the new dom return XMLUtilities.write(doc, new ExchangerOutputFormat("", false, "UTF-8")); }
From source file:com.cladonia.xml.XMLUtilities.java
License:Open Source License
/** * Parses the document for this URL, does not substitute & * and does resolve all XIncludes./*w ww.j a v a2s.c o m*/ * * @param url the URL of the document. * * @return the Dom4J document. */ public static synchronized XDocument parseWithoutSubstitution(URL url) throws IOException, SAXParseException { if (DEBUG) System.out.println("XMLUtilities.parseWithoutSubstitution( " + url + ")"); XDocument document = null; InputStream stream = URLUtilities.open(url); XMLReader reader = new XMLReader(stream); // create parser that resolves XIncludes. SAXParser parser = new SAXParser(new XIncludeParserConfiguration()); SAXReader saxer = new SAXReader(parser); // create parser that does not resolves XIncludes. // SAXReader saxer = createReader( false); saxer.setStripWhitespaceText(true); saxer.setMergeAdjacentText(true); try { document = parse(saxer, reader, url.toString()); } finally { stream.close(); reader.close(); } return document; }
From source file:com.cladonia.xml.XMLUtilities.java
License:Open Source License
/** * Resolves all XIncludes/* ww w .j a v a 2 s.c om*/ * * @param text The XML which may contain XIncludes * @param encoding The encoding * @param encoding The url of the file which can be used as a base identifier * * @return the XML with any XIncludes resolved */ public static String resolveXIncludes(String text, String encoding, URL url, ExchangerOutputFormat format) throws DocumentException, IOException { // set the paser to use XInclude configuration SAXParser parser = new SAXParser(new XIncludeParserConfiguration()); // create a dom4j SaxReader SAXReader reader = new SAXReader(parser); // if no encoding given then use UTF-8 if (encoding == null) { encoding = "UTF-8"; } // get the bytes from the input text ByteArrayInputStream stream = new ByteArrayInputStream(text.getBytes(mapXMLEncodingToJava(encoding))); InputStreamReader isReader = new InputStreamReader(stream, mapXMLEncodingToJava(encoding)); InputSource source = new InputSource(isReader); if (url != null) { source.setSystemId(url.toString()); } // using sax read the stream into a dom4j dom XDocument doc = (XDocument) reader.read(source); // write the new dom return write(doc, format, true); }
From source file:com.devoteam.srit.xmlloader.core.coding.binary.XMLDoc.java
License:Open Source License
/** * Parses the XMLFile against the XMLSchema * @throws java.lang.ParsingException//w w w . jav a 2 s. c o m */ public void parse() throws ParsingException { if (null == this.xmlFile) { throw new ParsingException("XML file not setted"); } final LinkedList<InputStream> streamsList = new LinkedList(); try { // // create a SchemaFactory capable of understanding WXS schemas // SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // // Load a XSD schema, represented by a Schema instance // final XMLDoc _this = this; factory.setResourceResolver(new LSResourceResolver() { public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { if (!systemId.toLowerCase().endsWith(".xsd")) { return null; } return new LSInput() { public InputStream getByteStream() { return null; } public Reader getCharacterStream() { return null; } public void setCharacterStream(Reader characterStream) { } public void setByteStream(InputStream byteStream) { } public String getStringData() { return ""; } public void setStringData(String stringData) { } public String getSystemId() { return ""; } public void setSystemId(String systemId) { } public String getPublicId() { return ""; } public void setPublicId(String publicId) { } public String getBaseURI() { return ""; } public void setBaseURI(String baseURI) { } public String getEncoding() { return "UTF-8"; } public void setEncoding(String encoding) { } public boolean getCertifiedText() { return false; } public void setCertifiedText(boolean certifiedText) { } }; } }); // // Create and configure the DocumentBuilderFactory // DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance(); parserFactory.setValidating(false); parserFactory.setNamespaceAware(true); parserFactory.setCoalescing(true); // // Get the parser // DocumentBuilder parser = parserFactory.newDocumentBuilder(); // // Parse to check document agains XSD // parser.setEntityResolver(new XMLLoaderEntityResolver(this.xmlFile)); parser.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException exception) throws SAXException { } public void error(SAXParseException exception) throws SAXException { throw exception; } public void fatalError(SAXParseException exception) throws SAXException { throw exception; } }); parser.parse(openInputStream(streamsList, this.xmlFile), this.getXMLFile().toString()); // // Instanciate the XML parser (no valididy check with dtd) // SAXReader reader = new SAXReader(false); reader.setEntityResolver(new XMLLoaderEntityResolver(this.xmlFile)); document = reader.read(openInputStream(streamsList, this.xmlFile), this.getXMLFile().toString()); } catch (SAXParseException e) { throw new ParsingException("In file : " + xmlFile + "\n" + "Parsing error at line " + e.getLineNumber() + ", column " + e.getColumnNumber() + "\n" + e.getMessage(), e); } catch (Exception e) { throw new ParsingException(e); } finally { for (InputStream stream : streamsList) { try { stream.close(); } catch (Exception e) { // ignore } } } }
From source file:com.devoteam.srit.xmlloader.core.Parameter.java
License:Open Source License
public void applyXPath(String xml, String xpath, boolean deleteNS) throws Exception { // remove beginning to '<' character int iPosBegin = xml.indexOf('<'); if (iPosBegin > 0) { xml = xml.substring(iPosBegin);/* w w w . j a v a 2 s . co m*/ } // remove from '>' character to the end int iPosEnd = xml.lastIndexOf('>'); if ((iPosEnd > 0) && (iPosEnd < xml.length() - 1)) { xml = xml.substring(0, iPosEnd + 1); } int iPosXMLLine = xml.indexOf("<?xml"); if (iPosXMLLine < 0) { xml = "<?xml version='1.0'?>" + xml; } // remove the namespace because the parser does not support them if there are not declare in the root node if (deleteNS) { xml = xml.replaceAll("<[a-zA-Z\\.0-9_]+:", "<"); xml = xml.replaceAll("</[a-zA-Z\\.0-9_]+:", "</"); } // remove doctype information (dtd files for the XML syntax) xml = xml.replaceAll("<!DOCTYPE\\s+\\w+\\s+\\w+\\s+[^>]+>", ""); InputStream input = new ByteArrayInputStream(xml.getBytes()); SAXReader reader = new SAXReader(false); reader.setEntityResolver(new XMLLoaderEntityResolver()); Document document = reader.read(input); XPath xpathObject = document.createXPath(xpath); Object obj = xpathObject.evaluate(document.getRootElement()); if (obj instanceof List) { List<Node> list = (List<Node>) obj; for (Node node : list) { add(node.asXML()); } } else if (obj instanceof DefaultElement) { Node node = (Node) obj; add(node.asXML()); } else if (obj instanceof DefaultAttribute) { Node node = (Node) obj; add(node.getStringValue()); } else if (obj instanceof DefaultText) { Node node = (Node) obj; add(node.getText()); } else { add(obj.toString()); } }
From source file:com.devoteam.srit.xmlloader.core.utils.Utils.java
License:Open Source License
public static Document stringParseXML(String xml, boolean deleteNS) throws Exception { // remove beginning to '<' character int iPosBegin = xml.indexOf('<'); if (iPosBegin > 0) { xml = xml.substring(iPosBegin);//from www . j a v a2s . c o m } // remove from '>' character to the end int iPosEnd = xml.lastIndexOf('>'); if ((iPosEnd > 0) && (iPosEnd < xml.length() - 1)) { xml = xml.substring(0, iPosEnd + 1); } int iPosXMLLine = xml.indexOf("<?xml"); if (iPosXMLLine < 0) { xml = "<?xml version='1.0'?>" + xml; } // remove the namespace because the parser does not support them if there are not declare in the root node if (deleteNS) { xml = xml.replaceAll("<[a-zA-Z\\.0-9_]+:", "<"); xml = xml.replaceAll("</[a-zA-Z\\.0-9_]+:", "</"); } // remove doctype information (dtd files for the XML syntax) xml = xml.replaceAll("<!DOCTYPE\\s+\\w+\\s+\\w+\\s+[^>]+>", ""); InputStream input = new ByteArrayInputStream(xml.getBytes()); SAXReader reader = new SAXReader(false); reader.setEntityResolver(new XMLLoaderEntityResolver()); Document document = reader.read(input); return document; }
From source file:com.devoteam.srit.xmlloader.core.utils.XMLDocument.java
License:Open Source License
/** * Parses the XMLFile against the XMLSchema * @throws java.lang.ParsingException//from w w w.ja v a 2 s. com */ public void parse() throws ParsingException { if (null == this.schemaFile) { throw new ParsingException("Schema file not setted"); } if (null == this.xmlFile) { throw new ParsingException("XML file not setted"); } final LinkedList<InputStream> streamsList = new LinkedList(); try { // // create a SchemaFactory capable of understanding WXS schemas // SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // // Load a XSD schema, represented by a Schema instance // Source schemaSource = new StreamSource(openInputStream(streamsList, this.schemaFile)); final XMLDocument _this = this; factory.setResourceResolver(new LSResourceResolver() { public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { if (!systemId.toLowerCase().endsWith(".xsd")) { return null; } final URI finalPath = URIFactory.resolve(_this.schemaFile, systemId);//"./" + schemaFile).substring(0, ("./" + schemaFile).lastIndexOf("/") + 1) + systemId; return new LSInput() { public InputStream getByteStream() { try { InputStream in = openInputStream(streamsList, finalPath); if (null == in) { GlobalLogger.instance().getApplicationLogger().error(TextEvent.Topic.CORE, "Exception : Include the XML file : ", finalPath); } return in; } catch (Exception e) { e.printStackTrace(); return null; } } public Reader getCharacterStream() { return null; } public void setCharacterStream(Reader characterStream) { } public void setByteStream(InputStream byteStream) { } public String getStringData() { return ""; } public void setStringData(String stringData) { } public String getSystemId() { return ""; } public void setSystemId(String systemId) { } public String getPublicId() { return ""; } public void setPublicId(String publicId) { } public String getBaseURI() { return ""; } public void setBaseURI(String baseURI) { } public String getEncoding() { return "UTF-8"; } public void setEncoding(String encoding) { } public boolean getCertifiedText() { return false; } public void setCertifiedText(boolean certifiedText) { } }; } }); Schema schema = factory.newSchema(schemaSource); // // Create and configure the DocumentBuilderFactory // DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance(); parserFactory.setSchema(schema); parserFactory.setValidating(false); parserFactory.setNamespaceAware(true); parserFactory.setCoalescing(true); // // Get the parser // DocumentBuilder parser = parserFactory.newDocumentBuilder(); // // Parse to check document agains XSD // parser.setEntityResolver(new XMLLoaderEntityResolver(this.xmlFile)); parser.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException exception) throws SAXException { } public void error(SAXParseException exception) throws SAXException { throw exception; } public void fatalError(SAXParseException exception) throws SAXException { throw exception; } }); parser.parse(openInputStream(streamsList, this.xmlFile), this.getXMLFile().toString()); // // Instanciate the XML parser (no valididy check with dtd) // SAXReader reader = new SAXReader(false); reader.setEntityResolver(new XMLLoaderEntityResolver(this.xmlFile)); document = reader.read(openInputStream(streamsList, this.xmlFile), this.getXMLFile().toString()); } catch (SAXParseException e) { throw new ParsingException("In file : " + xmlFile + "\n" + "Parsing error at line " + e.getLineNumber() + ", column " + e.getColumnNumber() + "\n" + e.getMessage(), e); } catch (Exception e) { throw new ParsingException(e); } finally { for (InputStream stream : streamsList) { try { stream.close(); } catch (Exception e) { // ignore } } } }