Changeset 203 in tmcsimulator-scriptbuilder for trunk/src/scriptbuilder/structures/XMLBuilder.java
- Timestamp:
- 02/01/2020 04:18:21 PM (6 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/scriptbuilder/structures/XMLBuilder.java
r201 r203 1 1 package scriptbuilder.structures; 2 2 3 import java.io.IOException; 4 import java.io.StringReader; 5 import java.io.StringWriter; 6 import javax.xml.parsers.DocumentBuilder; 7 import javax.xml.parsers.DocumentBuilderFactory; 8 import javax.xml.parsers.ParserConfigurationException; 9 import javax.xml.transform.OutputKeys; 10 import javax.xml.transform.Transformer; 11 import javax.xml.transform.TransformerException; 12 import javax.xml.transform.TransformerFactory; 13 import javax.xml.transform.dom.DOMSource; 14 import javax.xml.transform.stream.StreamResult; 15 import org.w3c.dom.Document; 16 import org.xml.sax.InputSource; 17 import org.xml.sax.SAXException; 18 3 19 /** 4 * 20 * Utility methods that build XML elements from content strings. 5 21 * @author Bryan McGuffin 6 22 */ 7 public class XML Writer23 public class XMLBuilder 8 24 { 9 25 … … 78 94 return output; 79 95 } 96 97 /** 98 * Prettyprint an XML string. 99 * @param xmlString (works best if it contains no newlines) 100 * @return String that has been nicely formatted with indentation 101 */ 102 public static String prettyPrintXML(String xmlString) 103 { 104 System.out.println("prettyprint start: " + xmlString); 105 Document xmlDoc = null; 106 String formattedXML = ""; 107 try { 108 xmlDoc = toXmlDocument(xmlString); 109 formattedXML = prettyprintdoc(xmlDoc); 110 } catch (ParserConfigurationException | SAXException | IOException 111 | TransformerException e) { 112 e.printStackTrace(); 113 } 114 return formattedXML; 115 } 116 117 118 /** 119 * Prettyprint an XML document. 120 * @param document an "ugly" XML document 121 * @return String nicely formatted XML with indentation 122 * @throws TransformerException 123 */ 124 public static String prettyprintdoc(Document document) 125 throws TransformerException { 126 TransformerFactory transformerFactory = TransformerFactory 127 .newInstance(); 128 Transformer transformer = transformerFactory.newTransformer(); 129 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 130 transformer.setOutputProperty( 131 "{http://xml.apache.org/xslt}indent-amount", "2"); 132 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 133 DOMSource source = new DOMSource(document); 134 StringWriter strWriter = new StringWriter(); 135 StreamResult result = new StreamResult(strWriter); 136 137 transformer.transform(source, result); 138 139 return strWriter.getBuffer().toString(); 140 141 } 142 143 /** Create an XML Document from a string that is in xml format. 144 * 145 * @param str xml format content 146 * @return Document containing the specified content. 147 * @throws ParserConfigurationException 148 * @throws SAXException 149 * @throws IOException 150 */ 151 public static Document toXmlDocument(String str) 152 throws ParserConfigurationException, SAXException, IOException { 153 154 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory 155 .newInstance(); 156 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 157 Document document = docBuilder.parse(new InputSource(new StringReader( 158 str))); 159 160 return document; 161 } 80 162 }
Note: See TracChangeset
for help on using the changeset viewer.
