解析一个XML,遇到点问题!!!求教!!!!!

malegebicaonima 2010-03-22 10:04:07
<a>
<b name = "hh">
<c>hhhh</c>
<d>kkk</d>
<e>
<f>ii</f>
</e>
</b>
<b name = "hh">
<c>pppp</c>
<d>uuu</d>
<e>
<f>jj</f>
</e>
</b>
</a>


这个xml,我想解析获得name="hh"的以下的节点信息,应该怎么写啊?


SAXReader saxReader=new SAXReader();

Document xmlDoc=saxReader.read(new File("qwe.xml"));

List list1=xmlDoc.selectNodes("//a/b/@name");

for(Iterator i=list1.iterator();i.hasNext();){

Attribute attribute = (Attribute) i.next();

if(attribute.getValue().equalsIgnoreCase("hh")){

往下怎么写啊?请指点一下!!!

}
}
...全文
169 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
malegebicaonima 2010-03-23
  • 打赏
  • 举报
回复
List list1=xmlDoc.selectNodes("//a//b[@name='1']");

我又找了一下,这个方法可以直接取到 b节点下,属性name=1的以下所有节点。。。
malegebicaonima 2010-03-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ramparter 的回复:]
饿。。


boolean b
...

for(...){
...
if(attribute.getValue().equalsIgnoreCase("hh")){ b=true}
if(b) 打印。。。
if(!(i.hasNext())) b=false
}
[/Quote]

哥们能再具体点不啊?实在是没看懂,你的意思!
ramparter 2010-03-22
  • 打赏
  • 举报
回复
饿。。


boolean b
...

for(...){
...
if(attribute.getValue().equalsIgnoreCase("hh")){ b=true}
if(b) 打印。。。
if(!(i.hasNext())) b=false
}




malegebicaonima 2010-03-22
  • 打赏
  • 举报
回复
...........
malegebicaonima 2010-03-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ramparter 的回复:]
方法设定遍历所有节点都需要打印属性。但是有个 开关b
设置个 boolean b=false (不打印)
如果 if判断成立 boolean开始变成ture...从此开始打印childrenNode....
如果返回根节点,或者父节点,重新置 b=false
[/Quote]

没懂!!!
ramparter 2010-03-22
  • 打赏
  • 举报
回复
方法设定遍历所有节点都需要打印属性。但是有个 开关b
设置个 boolean b=false (不打印)
如果 if判断成立 boolean开始变成ture...从此开始打印childrenNode....
如果返回根节点,或者父节点,重新置 b=false
malegebicaonima 2010-03-22
  • 打赏
  • 举报
回复
................
malegebicaonima 2010-03-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 laorer 的回复:]
如果用 dom4j的话,
一个 xpath就可以了

//**/@name="hh"

具体的还要查下
[/Quote]

你的方法我试了,路径里我用了"//**/@name="hh"/..."

不行啊
laorer 2010-03-22
  • 打赏
  • 举报
回复
如果用 dom4j的话,
一个 xpath就可以了

//**/@name="hh"

具体的还要查下
wenlong_wang 2010-03-22
  • 打赏
  • 举报
回复
。。。。。。。。。。。。。。。。
zyus1987 2010-03-22
  • 打赏
  • 举报
回复
给分~!
  • 打赏
  • 举报
回复
0.0
malegebicaonima 2010-03-22
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 zyus1987 的回复:]
试试吧~!应该才不多了~!虽然和你写的有些出入,但是基本功能是实现了~!
[/Quote]

哥们,谢谢你 !!!!

attribute.getParent().elements();

我要的就是这个,太感谢你了。。

学习了!!!!!!!!
zyus1987 2010-03-22
  • 打赏
  • 举报
回复
试试吧~!应该才不多了~!虽然和你写的有些出入,但是基本功能是实现了~!
zyus1987 2010-03-22
  • 打赏
  • 举报
回复

import java.io.File;
import java.util.Iterator;
import java.util.List;


import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class DomTest {
public DomTest() {
};


/**
* 递归遍历方法
*
* @param element
*/
public void getElementList(Element element) {
List elements = element.elements();
if (elements.size() == 0) {
// 没有子元素
String xpath = element.getPath();
String value = element.getTextTrim();
System.out.println("xpath:" + xpath);
System.out.println("value:" + value);
} else {
// 有子元素
for (Iterator it = elements.iterator(); it.hasNext();) {
Element elem = (Element) it.next();
// 递归遍历
getElementList(elem);
}
}
}

public void readElement(String xpath, String fileName) {
SAXReader saxReader = new SAXReader();
try {
Document xmlDoc = saxReader.read(new File(fileName));
List list1 = xmlDoc.selectNodes(xpath);
for (Iterator i = list1.iterator(); i.hasNext();) {
Attribute attribute = (Attribute) i.next();
if (attribute.getValue().equalsIgnoreCase("hh")) {
getElementList(attribute.getParent());
}
}
} catch (DocumentException e) {
e.printStackTrace();
}

}

public static void main(String args[]) {
DomTest domTest = new DomTest();
domTest.readElement("//a/b/@name", "dom4jTest.xml");
}
}

zyus1987 2010-03-22
  • 打赏
  • 举报
回复
import java.io.File;
import java.util.Iterator;
import java.util.List;


import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class DomTest {
public DomTest() {
};


/**
* 递归遍历方法
*
* @param element
*/
public void getElementList(Element element) {
List elements = element.elements();
if (elements.size() == 0) {
// 没有子元素
String xpath = element.getPath();
String value = element.getTextTrim();
System.out.println("xpath:" + xpath);
System.out.println("value:" + value);
} else {
// 有子元素
for (Iterator it = elements.iterator(); it.hasNext();) {
Element elem = (Element) it.next();
// 递归遍历
getElementList(elem);
}
}
}

public void readElement(String xpath, String fileName) {
SAXReader saxReader = new SAXReader();
try {
Document xmlDoc = saxReader.read(new File(fileName));
List list1 = xmlDoc.selectNodes(xpath);
for (Iterator i = list1.iterator(); i.hasNext();) {
Attribute attribute = (Attribute) i.next();
if (attribute.getValue().equalsIgnoreCase("hh")) {
getElementList(attribute.getParent());
}
}
} catch (DocumentException e) {
e.printStackTrace();
}

}

public static void main(String args[]) {
DomTest domTest = new DomTest();
domTest.readElement("//a/b/@name", "dom4jTest.xml");
}
}
malegebicaonima 2010-03-22
  • 打赏
  • 举报
回复
求助,帮忙解决一下,好吗!!?!?!?!?!!?
malegebicaonima 2010-03-22
  • 打赏
  • 举报
回复
我的问题问的不明白吗?我觉得我说的很清楚啊,我就是到红色部分,不知道怎么写了。。。
行舟 2010-03-22
  • 打赏
  • 举报
回复
/**
* <root>
* <!--利用dom4j创建document-->
* <book type="org.active.book">创建Document</book>
* </root>
* @desc:
* @throws Exception
*/
private void parse() throws Exception {
// JDK1.6自带的解析方式
javax.xml.parsers.DocumentBuilderFactory documentFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder builder = documentFactory.newDocumentBuilder();
org.w3c.dom.Document document = builder.parse(DomDocument.class.getResourceAsStream("/dom.xml"));
Node root = document.getFirstChild();
System.out.println("@JDK1.6 root name:"+root.getNodeName());
NodeList childs = root.getChildNodes();
for(int i=0;i<childs.getLength();i++){
Node node = childs.item(i);
if("book".equals(node.getNodeName())){
//取到节点时,取属性
NamedNodeMap map = node.getAttributes();
System.out.println("@JDK1.6 book node att:"+map.getNamedItem("type"));
}
}
}

自己参考做吧!
下面是DOM4J
/**
* @desc:利用String 字符串来生生成document
* @return
* @throws DocumentException
*/
private void createDocument(String str) throws Exception{
Document document =DocumentHelper.parseText(str);
Element root = document.getRootElement();
System.out.println("root name :"+root.getName());
System.out.println("root xml string :"+root.asXML());
Element child = root.element("book");
System.out.println("book xml String:"+child.asXML());
System.out.println("book's type: "+child.attributeValue("type"));
}
malegebicaonima 2010-03-22
  • 打赏
  • 举报
回复
0.0
加载更多回复(2)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧