Java使用dom4j解析XML字符串
对于XML文件或者字符串的解析有很多方法。
1、较简洁的是W3C,实例如下:
public void parseXmlByW3C(){
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
Document doc = null;
try {
db = dbf.newDocumentBuilder();
InputStream inputStream = ClassLoader.getSystemResourceAsStream("interface.xml");
doc = db.parse(inputStream);
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
NamedNodeMap nodes = doc.getElementsByTagName("Interface").item(0).getAttributes();
for(int i=0;i<nodes.getLength();i++){
System.out.println(nodes.item(i).getNodeName()+" : " +nodes.item(i).getNodeValue()+"\n");
}
NodeList nodeList = doc.getElementsByTagName("property");
for(int i=0;i<nodeList.getLength();i++){
System.out.println(nodeList.item(i).getAttributes().item(0).getNodeValue()+":"+nodeList.item(i).getTextContent()+"\n");
}
}
2、使用Dom4j解析XML字符串:
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public void parseXmlByDom4j(){
//需要解析的XML串
String strXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +"<Interface id=\"Test_V01\" content=\"output\" version=\"1.0\">"+"<property name=\"client_id\">test01</property>" +"<property name=\"project\">$ARG0</property>"+"</Interface>";
Map<Object, Object> map = new HashMap<Object, Object>();
Document doc = null;
try {
doc = DocumentHelper.parseText(strXml);
} catch (DocumentException e) {
//解析失败
e.printStackTrace();
}
if(doc==null)
return;
//获取跟节点
Element element = doc.getRootElement();
//获得根节点所有属性值
List<?> iList = element.attributes();
for(int i=0;i<iList.size();i++){
Attribute attribute = (Attribute)iList.get(i);
map.put(attribute.getName(), attribute.getValue());
}
//遍历根节点下属性为property的子节点
Iterator<?> pIterator = element.elementIterator("property");
while(pIterator.hasNext()){
Element ele = (Element)pIterator.next();
//子节点的name的值,和Text
map.put(ele.valueOf("@name"), ele.getText());
}
//循环输出
Iterator<Entry<Object, Object>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Entry<Object, Object> entry = iterator.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
在实际应用中,把W3C的解析方法放到Web工程中,总有解析错误,推荐使用Dom4j。
推荐阅读: