建议你用JDOM
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.input.*;
import org.jdom.output.*;public class JDOMBuildXMLDoc
{
public void BuildXMLDoc(OutputStream out)
throws IOException, JDOMException
{
Element eeeRoot, eee1, eee2;
Document Doc;
DocType DocT;
ProcessingInstruction PI; eeeRoot = new Element("employees_information");
Doc = new Document(eeeRoot); DocT = new DocType("employees_information", "DTDTest.dtd");
Doc.setDocType(DocT);
/*PI = new ProcessingInstruction("cocoon-process", "type='xsp'");
Doc.addContent(PI);
PI = new ProcessingInstruction("cocoon-process", "type='xslt'");
Doc.addContent(PI);*/ eeeRoot = Doc.getRootElement();
eee1 = new Element("name");
eee2 = eee1.setText("C.Y. Shen");
eee2 = eee1.addAttribute("emp_id", "001");
eee1 = eeeRoot.addContent(eee2); eee1 = new Element("age");
eee2 = eee1.setText("43");
eee1 = eeeRoot.addContent(eee2);
eee1 = new Element("sex");
eee2 = eee1.setText("Male");
eee1 = eeeRoot.addContent(eee2); eee1 = new Element("address");
eee2 = eee1.setText("3, Lane 121, Sec. 1, Hoping E. RD, Taipei");
eee1 = eeeRoot.addContent(eee2); eee1 = new Element("email");
eee2 = eee1.setText("[email protected]");
eee1 = eeeRoot.addContent(eee2); eee1 = new Element("tel");
eee2 = eee1.setText("&shen_tel;");
eee1 = eeeRoot.addContent(eee2); XMLOutputter XMLOut = new XMLOutputter();
XMLOut.output(Doc, out);
} public static void main(String[] args)
{
try
{
JDOMBuildXMLDoc JDOMBuildDocInstance = new JDOMBuildXMLDoc(); System.out.println("========================================");
System.out.println("Now we build an XML document .....");
System.out.println("========================================");
JDOMBuildDocInstance.BuildXMLDoc(System.out);
System.out.println("========================================");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.input.*;
import org.jdom.output.*;public class JDOMBuildXMLDoc
{
public void BuildXMLDoc(OutputStream out)
throws IOException, JDOMException
{
Element eeeRoot, eee1, eee2;
Document Doc;
DocType DocT;
ProcessingInstruction PI; eeeRoot = new Element("employees_information");
Doc = new Document(eeeRoot); DocT = new DocType("employees_information", "DTDTest.dtd");
Doc.setDocType(DocT);
/*PI = new ProcessingInstruction("cocoon-process", "type='xsp'");
Doc.addContent(PI);
PI = new ProcessingInstruction("cocoon-process", "type='xslt'");
Doc.addContent(PI);*/ eeeRoot = Doc.getRootElement();
eee1 = new Element("name");
eee2 = eee1.setText("C.Y. Shen");
eee2 = eee1.addAttribute("emp_id", "001");
eee1 = eeeRoot.addContent(eee2); eee1 = new Element("age");
eee2 = eee1.setText("43");
eee1 = eeeRoot.addContent(eee2);
eee1 = new Element("sex");
eee2 = eee1.setText("Male");
eee1 = eeeRoot.addContent(eee2); eee1 = new Element("address");
eee2 = eee1.setText("3, Lane 121, Sec. 1, Hoping E. RD, Taipei");
eee1 = eeeRoot.addContent(eee2); eee1 = new Element("email");
eee2 = eee1.setText("[email protected]");
eee1 = eeeRoot.addContent(eee2); eee1 = new Element("tel");
eee2 = eee1.setText("&shen_tel;");
eee1 = eeeRoot.addContent(eee2); XMLOutputter XMLOut = new XMLOutputter();
XMLOut.output(Doc, out);
} public static void main(String[] args)
{
try
{
JDOMBuildXMLDoc JDOMBuildDocInstance = new JDOMBuildXMLDoc(); System.out.println("========================================");
System.out.println("Now we build an XML document .....");
System.out.println("========================================");
JDOMBuildDocInstance.BuildXMLDoc(System.out);
System.out.println("========================================");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货