急救XML问题 java中

来源:百度知道 编辑:UC知道 时间:2024/09/21 09:31:13
例如:
<a>
<b>
<d name ='1'/>
</b>
<b1>
<d1 value ="1"/>
</b1>
<a>

我的要求是通过String 1 得到 b 和 b1 用String 返回
高分悬赏 谢谢大家
传一个String n =“1”
通过迭代各子节点属性“1” 得到他们的父节点 B和B1 用STRING 返回 B 和B1
________________________________
在方法中传个字符窜 “1”
根据“1” 找到XML中相匹配的字符 根据这个字符找到根结点

import java.util.ArrayList;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class TestXMLByJDOM {

private Document doc;
private List<String> result;

public TestXMLByJDOM(String fName){
try{
result = new ArrayList<String>();
doc = (new SAXBuilder()).build(fName);
}catch(Exception e){
e.printStackTrace();
}
}

public List<String> getElementsWithChildAttValue(String value){
Element root = doc.getRootElement();
try {
for(Element e : (List<Element>)root.getChildren()){
hasChildWithValue(e, "1");
}
} catch (Exception e) {
System.out.println("Can not parse XPATH of the given XML file");
}
return result;
}

public void hasChildWithValue(Eleme