谢啦,再问个问题,在 xerces 的包里面,有什么方法把一棵DOM Tree 生成xml file吗?
Transforming a DOM Tree to an XML Document To transform the DOM tree created in the previous section to an XML document, the following code fragment first creates a Transformer object that will perform the transformation. TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); Using the DOM tree root node, the following line of code constructs a DOMSource object as the source of the transformation. DOMSource source = new DOMSource(document); The following code fragment creates a StreamResult object to take the results of the transformation and transforms the tree into an XML file. File newXML = new File("newXML.xml"); FileOutputStream os = new FileOutputStream(newXML); StreamResult result = new StreamResult(os); transformer.transform(source, result);
要求不能用javax里面的Transformer类,只用xerces包里面的方法。
Document doc= new DocumentImpl(); Element root = doc.createElement("person"); // Create Root Element Element item = doc.createElement("name"); // Create element item.appendChild( doc.createTextNode("好大一朵棉花") ); root.appendChild( item ); // atach element to Root element item = doc.createElement("age"); // Create another Element item.appendChild( doc.createTextNode("26" ) ); root.appendChild( item ); // Attach Element to previous element down tree item = doc.createElement("height"); item.appendChild( doc.createTextNode("1.78" ) ); root.appendChild( item ); // Attach another Element - grandaugther doc.appendChild( root ); // Add Root to Document OutputFormat format = new OutputFormat( doc ); //Serialize DOM StringWriter stringOut = new StringWriter(); //Writer will be a String XMLSerializer serial = new XMLSerializer( stringOut, format ); serial.asDOMSerializer(); // As a DOM Serializer serial.serialize( doc.getDocumentElement() ); File newFile = new File("me.xml"); FileWriter file = new FileWriter(newFile); file.write(stringOut.toString()); file.close();
这个可以直接获得
To transform the DOM tree created in the previous section to an XML document, the following code fragment first creates a Transformer object that will perform the transformation. TransformerFactory transFactory =
TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer(); Using the DOM tree root node, the following line of code constructs a DOMSource object as the source of the transformation. DOMSource source = new DOMSource(document); The following code fragment creates a StreamResult object to take the results of the transformation and transforms the tree into an XML file. File newXML = new File("newXML.xml");
FileOutputStream os = new FileOutputStream(newXML);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);
Element root = doc.createElement("person"); // Create Root Element
Element item = doc.createElement("name"); // Create element
item.appendChild( doc.createTextNode("好大一朵棉花") );
root.appendChild( item ); // atach element to Root element
item = doc.createElement("age"); // Create another Element
item.appendChild( doc.createTextNode("26" ) );
root.appendChild( item ); // Attach Element to previous element down tree
item = doc.createElement("height");
item.appendChild( doc.createTextNode("1.78" ) );
root.appendChild( item ); // Attach another Element - grandaugther
doc.appendChild( root ); // Add Root to Document
OutputFormat format = new OutputFormat( doc ); //Serialize DOM
StringWriter stringOut = new StringWriter(); //Writer will be a String
XMLSerializer serial = new XMLSerializer( stringOut, format );
serial.asDOMSerializer(); // As a DOM Serializer serial.serialize( doc.getDocumentElement() ); File newFile = new File("me.xml");
FileWriter file = new FileWriter(newFile);
file.write(stringOut.toString());
file.close();
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.apache.xml.serialize.*;需要引入的包