关键是doc.createElement(String(element[i]))里element[i]都是null,你是不是需要从results里获得所需的element name阿,怎么觉得有点乱

解决方案 »

  1.   

    要工整一些,
    应该象我这样子写:)import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.FileWriter;import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;import org.apache.xml.serialize.OutputFormat;
    import org.apache.xml.serialize.XMLSerializer;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;public class DbToXML {
    private DocumentBuilder parser = null;
    private Document xmldoc = null; //构造函数, 创建空的XML内容
    public DbToXML() {
    DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
    try {
    this.parser = dbfactory.newDocumentBuilder();
    this.addRootNode();
    }catch (Exception exp) {
    this.parser = null;
    this.xmldoc = null;
    exp.printStackTrace();
    }
    } //装载文件, 如果文件不存在或者出错, 创建空的XML内容
    public void load(String strZL_CodeFile) throws Exception {
    if (this.parser == null) {
    throw new Exception("Parser initialize error!");
    }
    try {
    this.xmldoc = this.parser.parse(strZL_CodeFile);
    } catch (Exception e) {
    e.printStackTrace();
    this.xmldoc = this.parser.newDocument();
    }
    } //保存当前的XML内容到文件
    public void save(String strZL_CodeFile) throws Exception {
    if (this.parser == null) {
    throw new Exception("Parser initialize error!");
    }
    File outputFile = new File(strZL_CodeFile);
    OutputFormat outputFormat = new OutputFormat("XML", "gb2312", true);
    FileWriter fileWriter = new FileWriter(outputFile);
    XMLSerializer xmlSerializer = new XMLSerializer(fileWriter, outputFormat);
    xmlSerializer.asDOMSerializer();
    xmlSerializer.serialize(xmldoc.getDocumentElement());
    fileWriter.close();
    } //添加根节点
    public void addRootNode() throws Exception {
    this.xmldoc =this.parser.parse(new ByteArrayInputStream(new String("<?xml version=\"1.0\" ?><Root/>").getBytes()));
    } //添加一级节点
    public void addNode(String strName, String strValue) {
    Element ZL_Code = (Element) this.getNodeByName(strName);
    if (ZL_Code != null) {
    ZL_Code.setAttribute("Title", strName);
    ZL_Code.setAttribute("Href", strValue);
    } else {
    ZL_Code = xmldoc.createElement("TreeNode");
    ZL_Code.setAttribute("Title", strName);
    ZL_Code.setAttribute("Href", strValue);
    //ZL_Code.setAttribute("ChildNode", "0");
    xmldoc.getDocumentElement().appendChild(ZL_Code);
    }
    } //添加二级节点
    public boolean addChildNode(
    String strName,
    String strValue,
    String strParentName) {
    Element ChildZL_Code = (Element) this.getNodeByName(strName); if (ChildZL_Code != null) {
    return false;
    } ChildZL_Code = (Element) this.getChildNodeByName(strName);
    if (ChildZL_Code != null&& !this.getNodeAttributeValue(ChildZL_Code.getParentNode(),"Name").equals(strParentName)) {
    return false;
    } Element ZL_Code = (Element) this.getNodeByName(strParentName);
    if (ZL_Code == null) {
    return false;
    } else {
    //ZL_Code.setAttribute("ChildNode", "1");
    if (ChildZL_Code != null) {
    ChildZL_Code.setAttribute("Value", strValue);
    } else {
    ChildZL_Code = this.xmldoc.createElement("TreeNode");
    ChildZL_Code.setAttribute("Title", strName);
    ChildZL_Code.setAttribute("Href", strValue);
    ZL_Code.appendChild(ChildZL_Code);
    }
    }
    return true;
    } //移除节点
    public void removeNodeByName(String strName) {
    Node node = this.getNodeByName(strName);
    if (node == null) {
    node = this.getChildNodeByName(strName);
    }
    if (node != null) {
    Element parent = (Element) node.getParentNode();
    parent.removeChild(node);
    if (!parent.hasChildNodes()) {
    parent.setAttribute("ChildNode", "0");
    }
    }
    }
    //通过节点的Name属性查找节点
    private Node getNodeByName(String strName) {
    NodeList nlist = xmldoc.getElementsByTagName("TreeNode");
    Node node = null;
    for (int i = 0; i < nlist.getLength(); i++) {
    if (strName
    .equals(this.getNodeAttributeValue(nlist.item(i), "Title"))) {
    node = nlist.item(i);
    break;
    }
    }
    return node;
    } //通过节点的Name属性查找子节点
    private Node getChildNodeByName(String strName) {
    NodeList nlist = xmldoc.getElementsByTagName("TreeNode");
    Node node = null;
    for (int i = 0; i < nlist.getLength(); i++) {
    if (strName
    .equals(this.getNodeAttributeValue(nlist.item(i), "Title"))) {
    node = nlist.item(i);
    break;
    }
    }
    return node;
    } private String getNodeAttributeValue(Node node, String strName) {
    NamedNodeMap nmap = node.getAttributes();
    Node tmpnode = nmap.getNamedItem(strName);
    String rtStr = "";
    if (tmpnode != null) {
    rtStr = tmpnode.getNodeValue().trim();
    }
    //System.out.println(strName + "=" + rtStr);
    return rtStr;
    }
     public static void main(String[] args) {
      DbToXML obj = new DbToXML();
      try{
       obj.addRootNode();
       obj.addNode("类","类1");
       obj.addChildNode("项","类1项1","类");
       obj.addChildNode("目","类1项1目1","项");
       obj.addChildNode("节","类1项1目1节1","目");
       obj.addChildNode("点","类1项1目1节1点1","节");
       obj.save("D:\\TreeNode.xml");
      }catch(Exception e){
       e.printStackTrace();
      }
     }
     
    }
      

  2.   

    import org.apache.xml.serialize.OutputFormat;
    import org.apache.xml.serialize.XMLSerializer;
    这两个包在什么地方?
      

  3.   

    去这里下载啦 
    http://xml.apache.org/xalan-j/index.html
      

  4.   

    是这个包吗?xalan-j-current-bin
    我找了好久找不到 那两个包