其实xml文件就是普通的文本文件,但写普通的文本文件写吧.我这边就是这样做的.

解决方案 »

  1.   

    public static void addNode(Document doc, String filePath, String xPath, String nodeName,
    String nodeText) throws XMLParserException {
    try {
    if (doc == null || 
    filePath == null || filePath.equals("") || 
    xPath    == null || xPath.equals("")    || 
    nodeName == null || nodeName.equals("")) {
    return;
    }
    Element root = doc.getRootElement();
    List listNode = XPath.selectNodes(root, xPath);
    if (listNode == null || listNode.size() <= 0) {
    return;
    Element childNode = new Element(nodeName);
    if (nodeText != null && !nodeText.trim().equals("")) {
    childNode.setText(nodeText);
    }
    for (Object objNode : listNode) {
    Element parentNode = (Element) objNode;
    parentNode.addContent(childNode);
    }
    save(doc, filePath);
    } catch (Exception e) {

    }
    }private static void save(Document doc, String filePath) throws XMLParserException {
    if(doc == null || 
    filePath == null || filePath.equals("")) {
    return;
    }
    Format format = Format.getPrettyFormat();
        format.setEncoding(ENCODING);
        format.setIndent(INDENT);
    XMLOutputter outputter = new XMLOutputter(format);
    try {
    outputter.output(doc, new FileOutputStream(filePath));
    } catch (Exception e) {

    }
    }
      

  2.   

    动态生成XML
    http://www.cnblogs.com/huazi4995/articles/587059.html