1 6 7 package SOFA.Util; 8 9 import java.io.*; 10 import org.w3c.dom.*; 11 import org.xml.sax.*; 12 import javax.xml.parsers.*; 13 import javax.xml.transform.*; 14 import javax.xml.transform.dom.*; 15 import javax.xml.transform.stream.*; 16 17 21 public class XML 22 { 23 24 25 public XML() 26 { 27 } 28 29 public static Document parse(File file) throws SAXException, IOException 30 { 31 Document doc = null; 32 Element element; 33 try 34 { 35 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 36 DocumentBuilder builder = factory.newDocumentBuilder(); 37 doc = builder.parse(file); 38 element = doc.getDocumentElement(); 39 element.normalize(); 40 } 41 catch (ParserConfigurationException e) 42 { 43 System.out.println("ParserConfigurationException: " + e.getMessage()); 44 System.exit(1); 45 } 46 return doc; 47 } 48 49 public static Document parse(InputStream stream) throws SAXException, IOException 50 { 51 Document doc = null; 52 Element element; 53 try 54 { 55 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 56 DocumentBuilder builder = factory.newDocumentBuilder(); 57 doc = builder.parse(stream); 58 element = doc.getDocumentElement(); 59 element.normalize(); 60 } 61 catch (ParserConfigurationException e) 62 { 63 System.out.println("ParserConfigurationException: " + e.getMessage()); 64 System.exit(1); 65 } 66 return doc; 67 } 68 69 public static void dump(File file, Element element) throws IOException 70 { 71 FileOutputStream out = new FileOutputStream(file); 72 dump(out, element); 73 out.close(); 74 } 75 76 public static void dump(OutputStream stream, Element element) throws IOException 77 { 78 try 79 { 80 TransformerFactory tFactory = TransformerFactory.newInstance(); 81 Transformer transformer = tFactory.newTransformer(); 82 transformer.setOutputProperty("indent", "yes"); 83 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 84 DOMSource source = new DOMSource(element); 85 StreamResult result = new StreamResult(stream); 86 transformer.transform(source, result); 87 } catch (TransformerConfigurationException e) { 88 System.out.println("TransformerConfigurationException: " + e.getMessage() ); 89 System.exit(1); 90 } catch (TransformerException e) { 91 System.out.println("TransformerException: " + e.getMessage() ); 92 System.exit(1); 93 } 94 } 95 96 public static Document newDocument() 97 { 98 Document doc = null; 99 try 100 { 101 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 102 DocumentBuilder builder = factory.newDocumentBuilder(); 103 doc = builder.newDocument(); 104 } 105 catch (ParserConfigurationException e) 106 { 107 System.out.println("ParserConfigurationException: " + e.getMessage()); 108 System.exit(1); 109 } 110 return doc; 111 } 112 113 public static void newTextElement(Element element, String tagName, String content) 114 { 115 Document doc = element.getOwnerDocument(); 116 Element el = doc.createElement(tagName); 117 Text tx = doc.createTextNode(content); 118 el.appendChild(tx); 119 element.appendChild(el); 120 } 121 122 public static void newOneAttributeElement(Element element, String tagName, String attributeName, String attributeValue) 123 { 124 Document doc = element.getOwnerDocument(); 125 Element el = doc.createElement(tagName); 126 el.setAttribute(attributeName, attributeValue); 127 element.appendChild(el); 128 } 129 130 public static String getTextContent(Element element) 131 { 132 NodeList nl = element.getChildNodes(); 133 for (int i = 0; i < nl.getLength(); i++) 134 { 135 Node node = nl.item(i); 136 if (node.getNodeType() == Node.TEXT_NODE) return ((Text)node).getData(); 137 } 138 return ""; 139 } 140 141 } 142 | Popular Tags |