java语句如何获取XML文件的节点值

如题 我想通过先获取id再获取value的方法得到XML文件的节点值 请问下Java语句该如何编写呢

你说的是获取指定id下面的节点的属性值吧!
程序如下,其中equal(“”)双引号中的值为节点的属性值
属性值获取如下:
package saxbuilder.ibm.xmlproperty;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class XmlProperty {
public Element element;
String path = "D:\\xml\\a.xml";
public void getElementProperty() {
File file = new File(path);
try {
SAXReader reader = new SAXReader();
Document dc = reader.read(file);
Element e = dc.getRootElement();
// 节点值
Element child = (Element) e.getQName("COMMAND");
System.out.println(child.toString());
// 一级节点
for (Iterator iterator = e.elementIterator(); iterator.hasNext();) {
Element el = (Element) iterator.next();
// 一级节点的属性信息
for (Iterator iterator2 = el.attributeIterator(); iterator2
.hasNext();) {
Attribute attribute = (Attribute) iterator2.next();
if(attribute.getName().equals("bodyCategory")){
System.out.println("attribute.getName()一级节点的属性信息+"+attribute.getName()+attribute.getValue());
}else{
System.out.println("attribute.getName()一级节点的属性信息+"+attribute.getName()+attribute.getValue());
}

// 二级节点
for (Iterator iterator3 = el.elementIterator(); iterator3
.hasNext();) {
Element ele = (Element) iterator3.next();
// 二级节点的属性信息
for (Iterator iterator4 = ele.attributeIterator(); iterator4
.hasNext();) {
Attribute attribute1 = (Attribute) iterator4.next();
if(attribute1.getName().equals("cmdType")){
System.out.println("attribute1.getName()二级节点的属性信息+"+attribute1.getName()+"+"+attribute1.getValue());

}else{
System.out.println("attribute1.getName()二级节点的属性信息+"+attribute1.getName()+"+"+attribute1.getValue());
}

}
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-11-27
package org.lxh.xml.dom ;
import java.io.* ;
import org.w3c.dom.* ;
import javax.xml.parsers.* ;
public class DOMDemo {
public static void main(String args[]) throws Exception {
// 取得DocumentBuilderFactory类的对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance() ;
// 取得DocumentBuilder类的对象
DocumentBuilder build = factory.newDocumentBuilder() ;
Document doc = build.parse(new File("D:" + File.separator + "dom_demo_02.xml")) ;
NodeList nl = doc.getElementsByTagName("name") ;
// 输出节点
System.out.println("姓名:" + nl.item(0).getFirstChild().getNodeValue()) ;
}
}
第2个回答  2011-11-27
DOM4j

public static Map<String, String[]> getKeyValueFromXml(String xmlContent, String listPath, String keyName,
String... valName) {
Map<String, String[]> ret = new HashMap<String, String[]>();
org.dom4j.Document document = null;
try {
document = DocumentHelper.parseText(xmlContent);
} catch (DocumentException documentException) {
documentException.printStackTrace();
}
if (document != null) {
List<org.dom4j.Node> keyNodes = document.selectNodes(listPath);
for (org.dom4j.Node keyNode : keyNodes) {
if (keyNode instanceof Element) {
Element ele = (Element) keyNode;
String key = ele.elementText(keyName);
String[] val = new String[valName.length];
for (int i = 0; i < valName.length; i++) {
String val1 = ele.elementText(valName[i]);
val[i] = val1;
}

ret.put(key, val);
}
}
}

return ret;
}追问

用dom4j解析XML我知道 我是想问如何获取获取指定id下某一项的参数值 java语句该如何写

本回答被提问者采纳
相似回答