关于在JAVA 中DOM4j解析XML的问题 本帖最后由 nihao6154 于 2014-04-26 21:30:56 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果不是必须用dom4j,我建议楼主用xpath解析 第一层解析://pre:FieldArray 第二层解析://pre:FieldArray/pre:FIELD/[position()=" + i+ "]/@GroupID";如果等于-1;一级,然后递归找子节点 xpath语法我没测试,但是这样做肯定是能满足你的需求的 public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);// never forget this! DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("D:\\1.xml"); // 先取根 String itemStr = "//FieldArray/FIELD[@GroupID=-1]"; XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(itemStr); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 1; i <= nodes.getLength(); i++) { expr = xpath.compile("//FieldArray/FIELD[@GroupID=-1][position()=" + i + "]/@Index"); Object index_id = expr.evaluate(doc, XPathConstants.STRING); System.out.println("我是父节点,id=" + index_id.toString()); // 得到子节点 getSon("//FieldArray/FIELD[@GroupID=" + index_id.toString() + "]", doc, xpath); } } public static void getSon(String url, Document doc, XPath xpath) throws XPathExpressionException { XPathExpression expr = xpath.compile(url); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 1; i <= nodes.getLength(); i++) { System.out.println(url + "[position()=" + i + "]/@Index"); expr = xpath.compile(url + "[position()=" + i + "]/@Index"); Object index_id = expr.evaluate(doc, XPathConstants.STRING); System.out.println("我是子节点,index_id=" + index_id.toString()); // 得到子节点 getSon("//FieldArray/FIELD[@GroupID=" + index_id.toString() + "]", doc, xpath); } } 还有我想问下,如果用xpath解析的话,是要导入saxpath.jar就可以吧,还需要其他的jar包吗? 还有节点里的属性用xpath怎么取? 都打成jar包了,我也没仔细看,那个demo我测试了应该是体现级别的,我用的都是很基础的xpatch语法,网上很好找 jsp request乱码问题。。 用IO流删除一个文本文件,但提示程序正在运行无法删除,怎么办啊!下面是代码 sybase数据库乱码问题 如何通过form里面的select控件向两个文本框传送内容。 jxl 读excel文件日期类型出现异常 这样的网页防复制技术怎么做到呢[高手请进] 100分求解答:我想解析一个html页面,把把所有的超连接得到? JSP报错,请大侠帮忙分析出错原因 javascript问题。检查两个表单值时候不能比较 servlet的文件应该怎样组织成目录? SSH项目中,调用一个servlet,应该怎么配置 关于iframe子页面去父页面标签值出错问题,求高手指导
第一层解析://pre:FieldArray
第二层解析://pre:FieldArray/pre:FIELD/[position()=" + i+ "]/@GroupID";如果等于-1;一级,然后递归找子节点
xpath语法我没测试,但是这样做肯定是能满足你的需求的
ParserConfigurationException, SAXException, IOException
{
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);// never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("D:\\1.xml");
// 先取根
String itemStr = "//FieldArray/FIELD[@GroupID=-1]";
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile(itemStr);
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 1; i <= nodes.getLength(); i++)
{
expr = xpath.compile("//FieldArray/FIELD[@GroupID=-1][position()="
+ i + "]/@Index");
Object index_id = expr.evaluate(doc, XPathConstants.STRING);
System.out.println("我是父节点,id=" + index_id.toString());
// 得到子节点
getSon("//FieldArray/FIELD[@GroupID=" + index_id.toString() + "]",
doc, xpath);
}
} public static void getSon(String url, Document doc, XPath xpath)
throws XPathExpressionException
{
XPathExpression expr = xpath.compile(url);
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 1; i <= nodes.getLength(); i++)
{
System.out.println(url + "[position()=" + i + "]/@Index");
expr = xpath.compile(url + "[position()=" + i + "]/@Index");
Object index_id = expr.evaluate(doc, XPathConstants.STRING);
System.out.println("我是子节点,index_id=" + index_id.toString());
// 得到子节点
getSon("//FieldArray/FIELD[@GroupID=" + index_id.toString() + "]",
doc, xpath);
}
}