<map>
 <layer id=1>
</layer>
</map>
我引用了
import org.w3c.dom.*;
import javax.xml.parsers.*
请问是这样写的吗?
DocumentBuilderFactor factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder()
Document doc= builder.newDocument;
Element map=doc.createElement("map")
doc.appendChild(map);
Element layer=doc.createElement("layer")
layer.setAttribute("id","1");
map.appendChild(layer);

解决方案 »

  1.   

    那我通过jdom生产了document怎么再 转化为String ?
    是document 的 tostring方法吗?
      

  2.   

    暑假生产实习做了一个Java对象与XML的相互转换项目,以下有个用java生成XML的例子,你可以参考一下,你应该一看就懂了:
      

  3.   

    import org.w3c.dom.*; 
    import javax.xml.parsers.*; 
    import javax.xml.transform.*; 
    import javax.xml.transform.dom.DOMSource; 
    import javax.xml.transform.stream.StreamResult; 
    import java.io.*; 
    public class writexml{ 
     private Document document; 
     private String filename; public writexml(String name) throws ParserConfigurationException{ 
    filename=name; 
    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 
    DocumentBuilder builder=factory.newDocumentBuilder();
    document=builder.newDocument(); 

    public void toWrite(String mytitle,String mycontent){ 
    Element root=document.createElement("WorkShop"); 
    document.appendChild(root); 
    Element title=document.createElement("Title"); 
    title.appendChild(document.createTextNode(mytitle)); 
    root.appendChild(title); 
    Element content=document.createElement("Content"); 
    content.appendChild(document.createTextNode(mycontent)); 
    root.appendChild(content);} 
    public void toSave(){ 
    try{ 
    TransformerFactory tf=TransformerFactory.newInstance(); 
    Transformer transformer=tf.newTransformer(); 
    DOMSource source=new DOMSource(document); 
    transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312"); 
    transformer.setOutputProperty(OutputKeys.INDENT,"yes"); 
    PrintWriter pw=new PrintWriter(new FileOutputStream(filename)); 
    StreamResult result=new StreamResult(pw); 
    transformer.transform(source,result); 

    catch(TransformerException mye){ 
    mye.printStackTrace(); 

    catch(IOException exp){ 
    exp.printStackTrace(); 


    public static void main(String args[]){ 
    try{ 
    writexml myxml=new writexml("d:\\9.xml"); 
    myxml.toWrite("中文题目","中文内容"); 
    myxml.toSave(); 
    System.out.print("Your writing is successful!"); 

    catch(ParserConfigurationException exp){ 
    exp.printStackTrace(); 
    System.out.print("Your writing is failed!"); 



      

  4.   

    dom4j read xml
    package com.scjp;import java.io.File;
    import java.io.FileWriter;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;
    import org.dom4j.Node;
    public class ParserXmlDom4j
    {
        public final static String filePath = "com\\scjp\\MyXml.xml";    public Document parse(File file) throws DocumentException
        {
            SAXReader reader = new SAXReader();
            Document document = reader.read(file);        return document;
        }    public void getXml(Document document) throws DocumentException
        {
            Element root = document.getRootElement();
            Iterator it = root.elementIterator();        //Iterator it = root.elementIterator("name");
            //Iterator it = root.attributeIterator();
            while (it.hasNext())
            {
                Element sub = (Element) it.next();
            }
        }    public void getNode(Document document) throws DocumentException
        {
            //List list = document.selectNodes("//row/person");
            Node node = document.selectSingleNode("//row/person/name");
            String value = node.getText();
            System.out.println("value : " + value);
        }
        
        public void writeXml() throws Exception
        {
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("root");
        root.addComment("this is test xml file");
        ArrayList children = new ArrayList();
        Element ele1 = root.addElement("element1");
        ele1.addAttribute("attr1","a");
        ele1.addAttribute("attr2","b");
        ele1.setText("this is element1");
        Element ele2 = root.addElement("element2");
        ele2.addAttribute("attr21","c");
        ele2.setText("this is element2");
        
        XMLWriter writer = null;
        OutputFormat format = OutputFormat.createPrettyPrint();
        writer = new XMLWriter(new FileWriter(new File("com\\scjp\\MyXml3.xml")),format);
        writer.write(doc);
        writer.close();
        
        
        }
      
        public static void main(String[] args)
        {
            ParserXmlDom4j pxd = new ParserXmlDom4j();        try
            {
            
                File file = new File(filePath);
                Document document = pxd.parse(file);
               
                
                Element root = document.getRootElement();
                List list = root.selectNodes("//root/standards");
                for(Iterator it = list.iterator();it.hasNext();)
                {
                Element subElement = (Element)it.next();
                Attribute subAttr = subElement.attribute("type");
                if("a3".equals(subAttr.getValue()))
                {
                for(Iterator ita = subElement.elementIterator("standard");ita.hasNext();)
                {
                Element belement = (Element)ita.next();
                String sno = belement.attributeValue("sno");
                System.out.println("sno : "+sno );
                }
                /*
                List a3List = subElement.selectNodes("/@sno");
                for(Iterator it3 = a3List.iterator();it3.hasNext();)
                {
                Attribute attr3 = (Attribute)it3.next();
                String novalue = attr3.getValue();
                System.out.println("novalue : "+ novalue);
                }
                */
                }
                }
       
                pxd.writeXml();
                /*
                List list = root.selectNodes("//root/standards/standard/@sno");
                for(int i=0; i<list.size(); i++)
                {
                Attribute attrs = (Attribute)list.get(i);
                String no = attrs.getValue();
                System.out.println("no value : "+ no);
                }
                */
                /*
                for(Iterator it = root.elementIterator("author"); it.hasNext();)
                {
                Element sub = (Element)it.next();
                String name = sub.attributeValue("name");
                String location = sub.attributeValue("location");
                String fulName = sub.getText();
                System.out.println("name "+name+" location : "+ location+" fulName "+ fulName);
                }
                */
                //pxd.getNode(doc);
            //SAXReader reader = new SAXReader();
                //Document document = reader.read(filePath);
            /*
            List list = document.selectNodes("//c/a/b/@color");            for (Iterator iter = list.iterator(); iter.hasNext();)
                {
                    Attribute attribute = (Attribute) iter.next();
                    String color = attribute.getValue();
                    System.out.println("color : "+ color);
                    if (color.equals("255.255.0"))
                    {
                        attribute.setValue("0.0.0");
                    }
                }
               */
            
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }<?xml version="1.0" encoding="UTF-8"?><!-- 
    <xml-body>
    <config>
      <font>
          <name>HanZi</name>
          <size unit="pt">36</size>
      </font>
    </config>
    </xml-body><row>
    <person>
    <name>王小明</name>
    <college>信息学院</college>
    <telephone>6258113</telephone>
    <notes>男,1955年生,博士,95年调入海南大学</notes>
    </person>
    </row><c>
    <a name="a">
       <b color="255.255.0"/>
    </a>
    <a name="b">
       <b color="255.255.1"/>
    </a>
    </c><root>
      <author name="James" location="UK">James Strachan</author>
      <author name="Bob" location="US">Bob McWhirter</author>
    </root>
    --> <standards type="a3">
       <standard sno="1" svalue="400" />
       <standard sno="3" svalue="400" />
     </standards>
     <standards type="b9">
       <standard sno="1" svalue="300" />
     </standards>
      

  5.   

    jdom!!!!! XMLWriter writer = null;
        OutputFormat format = OutputFormat.createPrettyPrint();
        writer = new XMLWriter(new FileWriter(new File("MyXml.xml")),format);
        writer.write(doc);
        writer.close();