我记得用JAXP不能满足你的要求,用DOM吧。

解决方案 »

  1.   

    参考JAXP的JAVADOC,有这样的例子
      

  2.   

    呵呵,当然可以了,你要求保存文件吧?
    算了,贴你一个class好了,你可以参照一下。(不过这只是我的一各demo)
    package utility;import javax.xml.transform.stream.*;
    import javax.xml.transform.dom.*;
    import javax.xml.parsers.*;
    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
    import org.w3c.dom.*;
    import javax.xml.transform.*;
    import java.io.*;/**
     * Insert the type's description here.
     * Creation date: (2001-8-31 15:21:04)
     * @author: Administrator
     */public class XmlManager {
    private static org.w3c.dom.Document document;
    /**
     * XmlManager constructor comment.
     */
    public XmlManager()
    {
    super();
    fileName = "map.xml";
    loadFile(fileName);
    }
    /**
     * Insert the method's description here.
     * Creation date: (2001-9-1 11:49:29)
     */
    public void addMethod()
    {
    try
    {
    Element cityElem = document.createElement("AllElement");
    Element childElem = document.createElement("Element");
    childElem.appendChild(document.createTextNode("TextNode"));
    cityElem.appendChild(childElem); NodeList nodeList = document.getElementsByTagName("city");
    nodeList.item(0).appendChild(cityElem);
    saveFile(document, "map.xml");
    }
    catch (Exception e)
    {
    System.out.println(e);
    }}
    /**
     * Insert the method's description here.
     * Creation date: (2001-9-1 9:58:46)
     * @return org.w3c.dom.Document
     */
    public static org.w3c.dom.Document getDocument() {
    return document; 
    }
    /**
     * Insert the method's description here.
     * Creation date: (2001-8-31 16:07:12)
     * @return int
     */
    public int getInfo()
    {
    NodeList nodeList = document.getElementsByTagName("city");
    for (int i = 0; i < nodeList.getLength(); i++)
    {
    Node node = nodeList.item(i);
    NodeList nodeChildList = node.getChildNodes();
    for (int j = 0; j < nodeChildList.getLength(); j++)
    {
    Node nodeChild = nodeChildList.item(j);
    if (nodeChild.getNodeType() == Node.ELEMENT_NODE)
    {
    // System.out.println("");
    // System.out.println("ELEMENT_NODE: ======================= Start");
    DebugTool.traceMessage("Name : " + nodeChild.getNodeName());
    Node nodeChildText = nodeChild.getChildNodes().item(0);
    if (nodeChildText.getNodeType() == Node.TEXT_NODE)
    {
    DebugTool.traceMessage("ChildValue : " + nodeChildText.getNodeValue());
    }
    // System.out.println("ELEMENT_NODE: ======================= End");
    }
    }
    }
    return 0;
    }
    /**
     * Insert the method's description here.
     * Creation date: (2001-8-31 15:49:26)
     * @return int
     * @param fileName java.lang.String
     */
    public int loadFile(String fileName)
    {
    if (true) 
    {
    //
    }
    try
    {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    document = builder.parse(fileName);
    }
    catch (SAXException sxe)
    {
    // Error generated during parsing)
    Exception x = sxe;
    if (sxe.getException() != null)
    x = sxe.getException();
    x.printStackTrace();
    }
    catch (ParserConfigurationException pce)
    {
    // Parser with specified options can't be built
    pce.printStackTrace();
    }
    catch (IOException ioe)
    {
    // I/O error
    ioe.printStackTrace();
    }
    return 0;
    }
    /**
     * Insert the method's description here.
     * Creation date: (2001-8-31 16:19:45)
     * @param args java.lang.String[]
     */
    public static void main(String[] args)
    {
    XmlManager fileManager = new XmlManager();
    fileManager.loadFile("map.xml");
    // fileManager.getInfo();
    // fileManager.modMethod();}
    /**
     * Insert the method's description here.
     * Creation date: (2001-9-1 11:49:29)
     */
    public void modMethod()
    {
    try

    Element cityElem = document.createElement("AllElement");
    Element childElem = document.createElement("Element");
    childElem.appendChild(document.createTextNode("TextNode"));
    cityElem.appendChild(childElem); NodeList nodeList = document.getElementsByTagName("city");
    nodeList.item(0).appendChild(cityElem);
    saveFile(document, "map.xml");
    }
    catch (Exception e)
    {
    System.out.println(e);
    }}
    /**
     * Insert the method's description here.
     * Creation date: (2001-9-3 10:44:11)
     * @param document org.w3c.dom.Document
     * @param fileName java.lang.String
     */
    public void saveFile(Document doc, String fileName)
    {
    try

    TransformerFactory tranFactory = TransformerFactory.newInstance();
    Transformer tf = tranFactory.newTransformer();
    DOMSource dom = new DOMSource(doc); java.util.Properties properties = tf.getOutputProperties();
    properties.setProperty(OutputKeys.ENCODING, "GBK");
    tf.setOutputProperties(properties); OutputStreamWriter osw = 
    new OutputStreamWriter(
    new BufferedOutputStream(new FileOutputStream(fileName)), 
    "GBK"); 
    StreamResult sr = new StreamResult(osw);
    tf.transform(dom, sr);
    }
    catch (Exception e)
    {
    System.out.println(e);
    }
    }
    /**
     * Insert the method's description here.
     * Creation date: (2001-9-1 9:58:46)
     * @param newDocument org.w3c.dom.Document
     */
    public static void setDocument(org.w3c.dom.Document newDocument) {
    document = newDocument;

    private java.lang.String fileName;/**
     * Insert the method's description here.
     * Creation date: (2001-9-3 10:44:11)
     * @param document org.w3c.dom.Document
     * @param fileName java.lang.String
     */
    public void saveFile()
    {
    saveFile(document, fileName);
    }}
      

  3.   

    说明:
    我的XML DOM是在程序中生成的空的DOM, 要从数据库读数据来填充的.