补充一点就是不是显示完kingdom 在显示classid type 而是显示一个kingdom一个classid 一个type
完了在显示kingdom 这样重复显示

解决方案 »

  1.   

    补充一点就是不是显示完kingdom 在显示classid type 而是显示一个kingdom一个classid 一个type
    完了在显示kingdom 这样重复显示
      

  2.   

    package cn.ujjboy.answer.csdn;import java.io.IOException;
    import java.util.List;import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.Namespace;
    import org.jdom.input.SAXBuilder;public class ReadXML3 { public static void main(String[] args) throws JDOMException, IOException {
    SAXBuilder sb = new SAXBuilder();
    Document doc = sb.build("d:\\d.xml");
    Element root = doc.getRootElement();
    List list = root.getChildren();// 得到根节点NewDataSet
    if (list == null || list.size() == 0)
    System.out.println("空文件");
    for (int i = 0; i < list.size(); i++) {
    Element FVDL = (Element) list.get(i);// 得到fvdl同级的节点
    List list2 = FVDL.getChildren();
    for (int j = 0; j < list2.size(); j++) {
    Element Vulnerabilities = (Element) list2.get(j);
    List list3 = Vulnerabilities.getChildren();// 得到Vulnerabilities同级的节点
    for (int k = 0; k < list3.size(); k++) {
    Element Vulnerability = (Element) list3.get(k);
    List list4 = Vulnerability.getChildren();// 得到Vulnerability同级的节点
    for (int l = 0; l < list4.size(); l++) {
    Element ClassInfo = (Element) list4.get(l);// 得到ClassInfo同级的节点

    Element ClassID = ClassInfo.getChild("ClassID",Namespace.getNamespace("xmlns://www.fortifysoftware.com/schema/fvdl")) ;
    System.out.println(ClassID.getText());
    Element Kingdom = ClassInfo.getChild("Kingdom",Namespace.getNamespace("xmlns://www.fortifysoftware.com/schema/fvdl")) ;
    System.out.println(Kingdom.getText());
    Element Type = ClassInfo.getChild("Type",Namespace.getNamespace("xmlns://www.fortifysoftware.com/schema/fvdl")) ;
    System.out.println(Type.getText());
    }
    }
    }
    }
    }
    }
      

  3.   

    你的代码太繁琐了 老板肯定不会同意的 就是想简单一点的 最好还是用JDOM+xpath来实现
      

  4.   

    晕 我还说不要xpath呢
    来来来 换这个 package cn.ujjboy.answer.csdn;import java.io.IOException;
    import java.util.List;import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.Namespace;
    import org.jdom.input.SAXBuilder;
    import org.jdom.xpath.XPath;public class ReadXML4 { public static void main(String[] args) throws JDOMException, IOException {
    SAXBuilder sb = new SAXBuilder();
    Document doc = sb.build("d:\\d.xml");
    Element root = doc.getRootElement();

    XPath xpath = XPath.newInstance("//ns:ClassInfo");
    xpath.addNamespace("ns", "xmlns://www.fortifysoftware.com/schema/fvdl");
    List classInfos = xpath.selectNodes(doc);//得到classInfo的列表 for (int i = 0; i < classInfos.size(); i++) {
    Element ClassInfo = (Element) classInfos.get(i); //你要取该节点下其他的值就模仿下面的写吧
    Element ClassID = ClassInfo.getChild("ClassID", Namespace.getNamespace("xmlns://www.fortifysoftware.com/schema/fvdl"));
    System.out.println(ClassID.getText());
    Element Kingdom = ClassInfo.getChild("Kingdom", Namespace.getNamespace("xmlns://www.fortifysoftware.com/schema/fvdl"));
    System.out.println(Kingdom.getText());
    Element Type = ClassInfo.getChild("Type", Namespace.getNamespace("xmlns://www.fortifysoftware.com/schema/fvdl"));
    System.out.println(Type.getText());
    }
    }
    }