你说的是获取指定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();
}
}
}
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()) ;
}
}
DOM4j
public static Map
String... valName) {
Map
org.dom4j.Document document = null;
try {
document = DocumentHelper.parseText(xmlContent);
} catch (DocumentException documentException) {
documentException.printStackTrace();
}
if (document != null) {
List
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;
}