我太笨了 高手看看 补充一点就是不是显示完kingdom 在显示classid type 而是显示一个kingdom一个classid 一个type完了在显示kingdom 这样重复显示 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 补充一点就是不是显示完kingdom 在显示classid type 而是显示一个kingdom一个classid 一个type完了在显示kingdom 这样重复显示 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()); } } } } }} 你的代码太繁琐了 老板肯定不会同意的 就是想简单一点的 最好还是用JDOM+xpath来实现 晕 我还说不要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()); } }} javascript高手进!!谢谢了 工作第二天,MySql导不出数据 java 连接SQL2005无法登陆 spring 类继承多接口的问题 请教j2ee老鸟 java CacheRowSet 获取ORACLE10G的数据的时候 报异常Invalid scale size Cannot be less than zero 关于代码规范器 如何在容器外调用Tomcat的JDNI资源 ejb的问题,请高手指教 Druid配置方法(WAS) 谁能解开这个问题谁就牛逼了!!! 如何jsp实现点对点聊天
完了在显示kingdom 这样重复显示
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());
}
}
}
}
}
}
来来来 换这个 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());
}
}
}