📄 xmlutils.java
字号:
tryReset= false; } } /** * Get an empty new Document * * @return Document * @throws ParserConfigurationException if construction problems occur */ public static Document newDocument() throws ParserConfigurationException { DocumentBuilder db = null; try { db = getDocumentBuilder(); Document doc = db.newDocument(); return doc; } finally { if (db != null) { releaseDocumentBuilder(db); } } } /** * Get a new Document read from the input source * @return Document * @throws ParserConfigurationException if construction problems occur * @throws SAXException if the document has xml sax problems * @throws IOException if i/o exceptions occur */ public static Document newDocument(InputSource inp) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder db = null; try { db = getDocumentBuilder(); try { db.setEntityResolver(new DefaultEntityResolver()); } catch (Throwable t) { log.debug("Failed to set EntityResolver on DocumentBuilder", t); } try { db.setErrorHandler(new XMLUtils.ParserErrorHandler()); } catch (Throwable t) { log.debug("Failed to set ErrorHandler on DocumentBuilder", t); } Document doc = db.parse(inp); return doc; } finally { if (db != null) { releaseDocumentBuilder(db); } } } /** * Get a new Document read from the input stream * @return Document * @throws ParserConfigurationException if construction problems occur * @throws SAXException if the document has xml sax problems * @throws IOException if i/o exceptions occur */ public static Document newDocument(InputStream inp) throws ParserConfigurationException, SAXException, IOException { return XMLUtils.newDocument(new InputSource(inp)); } /** * Get a new Document read from the indicated uri * @return Document * @throws ParserConfigurationException if construction problems occur * @throws SAXException if the document has xml sax problems * @throws IOException if i/o exceptions occur */ public static Document newDocument(String uri) throws ParserConfigurationException, SAXException, IOException { // call the authenticated version as there might be // username/password info embeded in the uri. return XMLUtils.newDocument(uri, null, null); } /** * Create a new document from the given URI, use the username and password * if the URI requires authentication. * @param uri the resource to get * @param username basic auth username * @param password basic auth password * @throws ParserConfigurationException if construction problems occur * @throws SAXException if the document has xml sax problems * @throws IOException if i/o exceptions occur */ public static Document newDocument(String uri, String username, String password) throws ParserConfigurationException, SAXException, IOException { InputSource ins = XMLUtils.getInputSourceFromURI(uri, username, password); Document doc = XMLUtils.newDocument(ins); // Close the Stream if (ins.getByteStream() != null) { ins.getByteStream().close(); } else if (ins.getCharacterStream() != null) { ins.getCharacterStream().close(); } return doc; } private static String privateElementToString(Element element, boolean omitXMLDecl) { return DOM2Writer.nodeToString(element, omitXMLDecl); } /** * turn an element into an XML fragment * @param element * @return stringified element */ public static String ElementToString(Element element) { return privateElementToString(element, true); } /** * turn a whole DOM document into XML * @param doc DOM document * @return string representation of the document, including XML declaration */ public static String DocumentToString(Document doc) { return privateElementToString(doc.getDocumentElement(), false); } public static String PrettyDocumentToString(Document doc) { StringWriter sw = new StringWriter(); PrettyElementToWriter(doc.getDocumentElement(), sw); return sw.toString(); } public static void privateElementToWriter(Element element, Writer writer, boolean omitXMLDecl, boolean pretty) { DOM2Writer.serializeAsXML(element, writer, omitXMLDecl, pretty); } public static void ElementToStream(Element element, OutputStream out) { Writer writer = getWriter(out); privateElementToWriter(element, writer, true, false); } public static void PrettyElementToStream(Element element, OutputStream out) { Writer writer = getWriter(out); privateElementToWriter(element, writer, true, true); } public static void ElementToWriter(Element element, Writer writer) { privateElementToWriter(element, writer, true, false); } public static void PrettyElementToWriter(Element element, Writer writer) { privateElementToWriter(element, writer, true, true); } public static void DocumentToStream(Document doc, OutputStream out) { Writer writer = getWriter(out); privateElementToWriter(doc.getDocumentElement(), writer, false, false); } public static void PrettyDocumentToStream(Document doc, OutputStream out) { Writer writer = getWriter(out); privateElementToWriter(doc.getDocumentElement(), writer, false, true); } private static Writer getWriter(OutputStream os) { Writer writer = null; try { writer = new OutputStreamWriter(os, "UTF-8"); } catch (UnsupportedEncodingException uee) { log.error(Messages.getMessage("exception00"), uee); writer = new OutputStreamWriter(os); } return writer; } public static void DocumentToWriter(Document doc, Writer writer) { privateElementToWriter(doc.getDocumentElement(), writer, false, false); } public static void PrettyDocumentToWriter(Document doc, Writer writer) { privateElementToWriter(doc.getDocumentElement(), writer, false, true); } /** * Convert a simple string to an element with a text node * * @param namespace - element namespace * @param name - element name * @param string - value of the text node * @return element - an XML Element, null if no element was created */ public static Element StringToElement(String namespace, String name, String string) { try { Document doc = XMLUtils.newDocument(); Element element = doc.createElementNS(namespace, name); Text text = doc.createTextNode(string); element.appendChild(text); return element; } catch (ParserConfigurationException e) { // This should not occur throw new InternalException(e); } } /** * get the inner XML inside an element as a string. This is done by * converting the XML to its string representation, then extracting the * subset between beginning and end tags. * @param element * @return textual body of the element, or null for no inner body */ public static String getInnerXMLString(Element element) { String elementString = ElementToString(element); int start, end; start = elementString.indexOf(">") + 1; end = elementString.lastIndexOf("</"); if (end > 0) return elementString.substring(start,end); else return null; } public static String getPrefix(String uri, Node e) { while (e != null && (e.getNodeType() == Element.ELEMENT_NODE)) { NamedNodeMap attrs = e.getAttributes(); for (int n = 0; n < attrs.getLength(); n++) { Attr a = (Attr)attrs.item(n); String name; if ((name = a.getName()).startsWith("xmlns:") && a.getNodeValue().equals(uri)) { return name.substring(6); } } e = e.getParentNode(); } return null; } /** * Searches for the namespace URI of the given prefix in the given DOM range. * * The namespace is not searched in parent of the "stopNode". This is * usefull to get all the needed namespaces when you need to ouput only a * subtree of a DOM document. * * @param prefix the prefix to find * @param e the starting node * @param stopNode null to search in all the document or a parent node where the search must stop. * @return null if no namespace is found, or the namespace URI. */ public static String getNamespace(String prefix, Node e, Node stopNode) { while (e != null && (e.getNodeType() == Node.ELEMENT_NODE)) { Attr attr = null; if (prefix == null) { attr = ((Element) e).getAttributeNode("xmlns"); } else { attr = ((Element) e).getAttributeNodeNS(Constants.NS_URI_XMLNS, prefix); } if (attr != null) return attr.getValue(); if (e == stopNode) return null; e = e.getParentNode(); } return null; } public static String getNamespace(String prefix, Node e) { return getNamespace(prefix, e, null); } /** * Return a QName when passed a string like "foo:bar" by mapping * the "foo" prefix to a namespace in the context of the given Node. * * @return a QName generated from the given string representation */ public static QName getQNameFromString(String str, Node e) { return getQNameFromString(str, e, false); } /** * Return a QName when passed a string like "foo:bar" by mapping * the "foo" prefix to a namespace in the context of the given Node. * If default namespace is found it is returned as part of the QName. * * @return a QName generated from the given string representation */ public static QName getFullQNameFromString(String str, Node e) { return getQNameFromString(str, e, true); } private static QName getQNameFromString(String str, Node e, boolean defaultNS) { if (str == null || e == null) return null; int idx = str.indexOf(':'); if (idx > -1) { String prefix = str.substring(0, idx); String ns = getNamespace(prefix, e); if (ns == null) return null; return new QName(ns, str.substring(idx + 1)); } else { if (defaultNS) { String ns = getNamespace(null, e); if (ns != null) return new QName(ns, str); } return new QName("", str); } } /** * Return a string for a particular QName, mapping a new prefix * if necessary. */ public static String getStringForQName(QName qname, Element e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -