📄 xmlhelper.java
字号:
package com.gzrealmap.oa.servlet;/* File: XMLHelper.java * Author: Jared Jackson <jjared@almaden.ibm.com> * Date: January 10, 2002 * Project: developerWorks XSL Callback Article */import org.w3c.dom.*;//import org.w3c.tidy.*; The tidy function is disabled for this articleimport org.xml.sax.*;import org.apache.xerces.dom.*;import org.apache.xerces.parsers.*;import org.apache.xalan.serialize.*;import org.apache.xalan.templates.*;import org.apache.xalan.processor.*;import org.apache.xml.serialize.*;import javax.xml.parsers.*;import javax.xml.transform.*;import javax.xml.transform.stream.*;import javax.xml.transform.dom.*;import java.io.*;import java.util.*;import java.net.*;public class XMLHelper { public static Document createXml(String rootName) { Document doc = new DocumentImpl(); doc.appendChild(doc.createElement(rootName)); return doc; } public static Document parseXMLFromURLString(String url) throws XMLHelperException { return parseXMLFromURL(convertStringToURL(url)); } public static Document parseXMLFromFile(File f) throws XMLHelperException { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(f); } catch (IOException ex) { throw new XMLHelperException("Unable to read from source string", ex); } catch (ParserConfigurationException ex) { throw new XMLHelperException("Unable to configure the parser", ex); } catch (SAXException ex) { throw new XMLHelperException("Unable to parse the input", ex); } } public static Document parseXMLFromURL(URL url) throws XMLHelperException { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); URLConnection inConnection = url.openConnection(); return builder.parse(inConnection.getInputStream()); } catch (IOException ex) { throw new XMLHelperException("Unable to read from source string", ex); } catch (ParserConfigurationException ex) { throw new XMLHelperException("Unable to configure the parser", ex); } catch (SAXException ex) { throw new XMLHelperException("Unable to parse the input", ex); } } public static Document parseXMLFromString(String source) throws XMLHelperException { InputSource is = new InputSource(new StringReader(source)); return parseXMLFromInputSource(is); } private static Document parseXMLFromInputSource(InputSource is) throws XMLHelperException { Document doc = null; try { DOMParser parser = new DOMParser(); parser.parse(is); doc = parser.getDocument(); } catch (IOException ioe) { throw new XMLHelperException("Unable to read from source string", ioe); } catch (SAXException saxe) { throw new XMLHelperException("Unable to parse the given string", saxe); } return doc; } public static Document transformXML(Document xmlDoc, Document xslDoc) throws XMLHelperException { try { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new DOMSource(xslDoc)); DOMResult result = new DOMResult(); transformer.transform(new DOMSource(xmlDoc), result); org.w3c.dom.Node resultNode = result.getNode(); if (resultNode instanceof Document) return (Document)resultNode; else return result.getNode().getOwnerDocument(); } catch (TransformerConfigurationException ex) { throw new XMLHelperException("Unable to perform transform " + ex.getLocationAsString(), ex); } catch (TransformerException ex) { throw new XMLHelperException("Unable to perform transform " + ex.getLocationAsString(), ex); } } public static void outputXML(Document doc, PrintStream stream) throws XMLHelperException { try { OutputFormat of = new OutputFormat(doc); of.setIndenting(true); XMLSerializer serializer = new XMLSerializer(stream, of); serializer.serialize(doc); } catch (IOException ioe) { throw new XMLHelperException("Unable to write to the given print stream", ioe); } } public static void outputTextToFile(String text, String fileName) throws XMLHelperException { try { File f = new File(fileName); File dir = new File(f.getParent()); dir.mkdirs(); FileWriter fw = new FileWriter(f); fw.write(text); fw.flush(); fw.close(); } catch (IOException ioe) { throw new XMLHelperException("Unable to write to the given file", ioe); } } public static void outputXMLToFile(Document doc, String fileName) throws XMLHelperException { try { if (doc == null) throw new IOException("Output XML document was null"); OutputFormat of = new OutputFormat(doc, "UTF-8", true); File f = new File(fileName); FileOutputStream fos = new FileOutputStream(f); XMLSerializer serializer = new XMLSerializer(fos, of); serializer.serialize(doc); fos.close(); } catch (IOException ioe) { throw new XMLHelperException("Unable to write to the given file", ioe); } } public static String convertXMLToString(Document doc) throws XMLHelperException { try { OutputFormat of = new OutputFormat(doc); of.setIndenting(true); StringWriter sw = new StringWriter(); XMLSerializer serializer = new XMLSerializer(sw, of); serializer.serialize(doc); return sw.toString(); } catch (IOException ioe) { throw new XMLHelperException("Unable to write to the string", ioe); } } /** * Copies the children of the mergeFromXML element and places them as children * in the mergeToXML. Of course it recursively gets the children as well. If childrenOnly * is set to false, just insert the mergeFromXML under the mergeToXML. */ public static void mergeXML(Element mergeToXML, Element mergeFromXML, boolean childrenOnly) { Document toDoc = mergeToXML.getOwnerDocument(); Element copyElem = (Element)(toDoc.importNode(mergeFromXML,true)); if (childrenOnly) { NodeList nlist = copyElem.getChildNodes(); for (int i=0; i < nlist.getLength(); i++) { org.w3c.dom.Node n = nlist.item(i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -