package ball.Xml; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Comment; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; public class CreateXml { private String output; public static void main(String[] args ) throws TransformerConfigurationException, IOException{ new CreateXml(); } public CreateXml() throws TransformerConfigurationException, IOException{ System.out.println("Create Element"); output = "output.xml"; CreateElement(); ReadXml(); } private void CreateElement() { try { ///////////////////////////// //Creating an empty XML Document //We need a Document DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.newDocument(); //////////////////////// //Creating the XML tree //create the root element and add it to the document Element root = doc.createElement("root"); doc.appendChild(root); //create a comment and put it in the root element Comment comment = doc.createComment("Just a thought"); root.appendChild(comment); //create child element, add an attribute, and add to root Element child = doc.createElement("child"); root = addAttribute(root,child,"a","b"); root = addAttribute(root,child,"x","a"); root = addAttribute(root,child,"x","a"); //add a text element to the child Text text = doc.createTextNode("Filler, ... I could have had a foo!"); child.appendChild(text); Element info = doc.createElement("Information"); root.appendChild(info); root = addAttribute(root, info, "Name", "Wongsakorn"); info.appendChild(doc.createTextNode("Hello")); root = addAttribute(root, info, "SureName", "Kongosod"); ///////////////// //Output the XML //set up a transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new File(output)); DOMSource source = new DOMSource(doc); trans.transform(source, result); } catch (Exception e) { System.out.println(e); } } private void ReadXml() throws TransformerConfigurationException, IOException { //print xml System.out.println("Here's the xml:\n"); InputStream ins = new FileInputStream(new File(output)); DataInputStream data = new DataInputStream(ins); BufferedReader reader = new BufferedReader(new InputStreamReader(data)); String strLine = ""; while ((strLine = reader.readLine())!=null) { System.out.println(strLine); } } private Element addAttribute(Element root, Element child, String name,String value) { child.setAttribute(name, value); root.appendChild(child); return root; } }
output:
<root>
<!--Just a thought-->
<child a="b" x="a">Filler, ... I could have had a foo!</child>
<Information Name="Wongsakorn" SureName="Kongosod">Hello</Information>
</root>
ไม่มีความคิดเห็น:
แสดงความคิดเห็น