下面的示例程序:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Dom
{
public static void main( String[] args ) throws Throwable
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware( true );
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element root = doc.createElement( "root" );
root.setAttribute( "xmlns:m" , "http://www.lfinance.fr/blog-rachat-credits" );
root.setAttribute( "xmlns:rt", "http://www.lfinance.fr/forum-rachat-credits" );
doc.appendChild( root );
Element elt = doc.createElement( "simple" );
elt.setAttribute( "m:FC_TargetPath" , "false" );
elt.setAttribute( "m:FC_KeepInContent", "false" );
elt.setAttribute( "rt:filterable" , "false" );
root.appendChild( doc.createTextNode( "\n\t" ));
root.appendChild( elt );
root.appendChild( doc.createTextNode( "\n" ));
TransformerFactory.newInstance().newTransformer().transform(
new DOMSource( doc ),
new StreamResult( System.out ));
}
}
输出:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root
xmlns:m="http://www.lfinance.fr/blog-rachat-credits"
xmlns:rt="http://www.lfinance.fr/forum-rachat-credits">
<simple
m:FC_KeepInContent="false"
m:FC_TargetPath="false"
rt:filterable="false" />
</root>