1 // Copyright Hugh Perkins 2006, 2009 2 // hughperkins@gmail.com http://manageddreams.com 3 // 4 // This program is free software; you can redistribute it and/or modify it 5 // under the terms of the GNU General Public License as published by the 6 // Free Software Foundation; either version 2 of the License, or 7 // (at your option) any later version. 8 // 9 // This program is distributed in the hope that it will be useful, but 10 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 11 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 // more details. 13 // 14 // You should have received a copy of the GNU General Public License along 15 // with this program in the file licence.txt; if not, write to the 16 // Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- 17 // 1307 USA 18 // You can find the licence also on the web at: 19 // http://www.opensource.org/licenses/gpl-license.php 20 // 21 // ====================================================================================== 22 // 23 24 package hughai.utils; 25 26 import org.w3c.dom.*; 27 import java.io.*; 28 import javax.xml.parsers.*; 29 import javax.xml.transform.*; 30 import javax.xml.transform.dom.*; 31 import javax.xml.transform.stream.*; 32 import javax.xml.xpath.*; 33 import java.util.*; 34 35 public class XmlHelper 36 { CreateDom()37 public static Document CreateDom() 38 { 39 Document newdoc = getDocumentBuilder().newDocument(); 40 //newdoc..PreserveWhitespace = true; 41 Element childnode = newdoc.createElement( "root" ); 42 newdoc.appendChild( childnode ); 43 return newdoc; 44 } getDocumentBuilder()45 static DocumentBuilder getDocumentBuilder(){ 46 DocumentBuilder documentBuilder = null; 47 try { 48 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 49 documentBuilder = documentBuilderFactory.newDocumentBuilder(); 50 } catch( Exception e ) { 51 throw new RuntimeException( e ); 52 } 53 return documentBuilder; 54 } 55 OpenDom( String sfilepath )56 public static Document OpenDom( String sfilepath ) 57 { 58 Document newdoc = null; 59 try { 60 newdoc = getDocumentBuilder().parse(new File(sfilepath)); 61 } catch( Exception e ){ 62 throw new RuntimeException(e); 63 } 64 return newdoc; 65 } SaveDom( Document dom, String filepath )66 public static void SaveDom( Document dom, String filepath ) { 67 try { 68 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 69 Transformer transformer = transformerFactory.newTransformer(); 70 71 DOMSource domSource = new DOMSource(dom); 72 73 StreamResult streamResult = new StreamResult(new FileOutputStream(filepath)); 74 transformer.setOutputProperty( OutputKeys.INDENT, "yes" ); 75 transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" ); 76 transformer.transform( domSource, streamResult ); 77 } catch( Exception e ) { 78 throw new RuntimeException( e ); 79 } 80 } AddChild( Element parent, String nodename )81 public static Element AddChild( Element parent, String nodename ) 82 { 83 Element childnode = parent.getOwnerDocument().createElement( nodename ); 84 parent.appendChild( childnode ); 85 return childnode; 86 } SelectElements( Element parent, String expression )87 public static List<Element> SelectElements( Element parent, String expression ) { 88 List<Element> nodes = new ArrayList<Element>(); 89 XPath xpath = getXPath(); 90 try{ 91 XPathExpression xpathExpression = xpath.compile(expression); 92 NodeList nodeList = (NodeList) xpathExpression.evaluate(parent, XPathConstants.NODESET); 93 for( int i = 0; i < nodeList.getLength(); i++ ) { 94 nodes.add( (Element)(nodeList.item(i))); 95 } 96 } catch ( Exception e ){ 97 throw new RuntimeException( e ); 98 } 99 return nodes; 100 } SelectSingleElement( Element parent, String expression )101 public static Element SelectSingleElement( Element parent, String expression ) { 102 Element element = null; 103 XPath xpath = getXPath(); 104 try{ 105 XPathExpression xpathExpression = xpath.compile(expression); 106 element = (Element) xpathExpression.evaluate(parent, XPathConstants.NODE); 107 } catch ( Exception e ){ 108 throw new RuntimeException( e ); 109 } 110 return element; 111 } getXPath()112 public static XPath getXPath(){ 113 return getXPathFactory().newXPath(); 114 } getXPathFactory()115 public static XPathFactory getXPathFactory(){ 116 return XPathFactory.newInstance(); 117 } 118 } 119