比如现在我有一个节点:<root>
</root>还有另外一个节点:<cld>
  <a>
    <c></c>
  </a>
  <b></b>
</cld>在dom4j 中没有类似 jdom 的 import node的方法
问:
dom4j 怎么实现把 下面那一大段节点加到root下面??

解决方案 »

  1.   


    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Attr;
    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;
    import org.xml.sax.SAXException;public class XmlReader02 {

    public static void parseXml(InputStream is){
    //工厂,得到一个DocumentBuilderFactory 实例
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance() ;
    try {
    //DocumentBuilder   生产Document对象的工人
    DocumentBuilder db = dbf.newDocumentBuilder() ;
    //Document xml文件在内存中存在的形式
    Document document = db.parse(is);
    //得到根元素标签内容
    Element root = document.getDocumentElement() ;

    printNode(root) ;


    } catch (ParserConfigurationException e) {
    e.printStackTrace();
    } catch (SAXException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private static void printNode(Node node){
    //得到这个节点的类型
    short nodeType = node.getNodeType();
    switch(nodeType){
    case Node.TEXT_NODE: //文本标签,直接打印
    System.out.print(node.getNodeValue());
    break;
    case Node.ELEMENT_NODE: //元素标签,先检查它的属性,并打印,再检查它的子标签(也需要检查属性和子标签    用递归)
    Element element = (Element)node ;//强转
    String nodeName = element.getNodeName() ;//得到标签名字
    System.out.print("<"+nodeName);//打印

    NamedNodeMap nnm = node.getAttributes();//得到这个标签里面的属性(不止一个,用map接收)
    for(int i = 0 ; i<nnm.getLength() ; i++){
    Attr attr = (Attr)nnm.item(i);
    printNode(attr) ;//其实是执行case Node.ATTRIBUTE_NODE
    }
    System.out.print(">");
    //以上就打印了一个标签 比如  <person pid="p02">

    //打印子标签 递归
    NodeList childNodeList = element.getChildNodes();
    for(int i = 0 ; i<childNodeList.getLength() ; i++){
    printNode(childNodeList.item(i));
    }

    System.out.print("</"+nodeName+">");
    //封尾   比如</person>
    break;
    case Node.ATTRIBUTE_NODE:
    Attr attr = (Attr)node ;
    String attrName = attr.getNodeName();
    String attrValue = attr.getNodeValue() ;
    System.out.print(" "+attrName+"=\""+attrValue+"\" ");
    break;
    }

    }

    public static void main(String[] args) throws FileNotFoundException {
    XmlReader02.parseXml(new FileInputStream("src/school.xml")) ;
    }
    }
    刚写的,带注释,我也不太懂<?xml version="1.0" encoding="gbk"?><school>
    <students>
    <student sid="s01" pwd="ok">
    <sname>Jim</sname>
    <address>USA</address>
    <classid>c01</classid>
    </student>
    <student sid="s02" pwd="ok">
    <sname>Tom</sname>
    <address>USA</address>
    <classid>c02</classid>
    </student>
    <student sid="s03" pwd="ok">
    <sname>Lucy</sname>
    <address>Beijing</address>
    <classid>c02</classid>
    </student>
    </students>
    <classes>
    <class cid="c01" cname="java">
    <headermaster>tea01</headermaster>
    <studentcount>50</studentcount>
    </class>
    <class cid="c02" cname="SQL">
    <headermaster>tea02</headermaster>
    <studentcount>30</studentcount>
    </class>
    </classes>
    <teachers>
    <teacher tid="tea01" tname="霍金">
    <gender>男</gender>
    <age>55</age>
    <country>美国</country>
    </teacher>
    <teacher tid="tea02" tname="盖茨">
    <gender>男</gender>
    <age>58</age>
    <country>美国</country>
    </teacher>
    </teachers>
    </school>
    这是上面main方法里面的school.xml
      

  2.   

    DOM4J的使用
    把下面的XML读出来,再加到第一个XML就可以了