提供一种简单的方法: String url = "http://xxx" SAXReader saxReader = new SAXReader(); org.dom4j.Document document = saxReader.read(url); String result = document.getRootElement().elemen("RESULT").getTextTrim();//获得根节点RESULT 的内容
使用Dom4j来解析比较不错:http://dom4j.sourceforge.net/dom4j-1.6.1/guide.html 支持字符串xml格式的内容 If you have a reference to a Document or any other Node such as an Attribute or Element, you can turn it into the default XML text via the asXML() method. Document document = ...; String text = document.asXML(); If you have some XML as a String you can parse it back into a Document again using the helper method DocumentHelper.parseText() String text = "<person> <name>James</name> </person>"; Document document = DocumentHelper.parseText(text);
SAXReader reader = new SAXReader();
doc = reader.read(new ByteArrayInputStream(内容.getBytes()));后面就是正常解析了
SAXReader saxReader = new SAXReader();
org.dom4j.Document document = saxReader.read(url);
String result = document.getRootElement().elemen("RESULT").getTextTrim();//获得根节点RESULT 的内容
支持字符串xml格式的内容
If you have a reference to a Document or any other Node such as an Attribute or Element, you can turn it into the default XML text via the asXML() method. Document document = ...;
String text = document.asXML();
If you have some XML as a String you can parse it back into a Document again using the helper method DocumentHelper.parseText() String text = "<person> <name>James</name> </person>";
Document document = DocumentHelper.parseText(text);
import java.io.StringReader;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;import com.ilongman.acs.exception.XmlException;public class XmlParser {
private static Log log = LogFactory.getLog(XmlParser.class);
private Document xmlDoc = null; public XmlParser() { } public XmlParser(String xml) throws XmlException{
init(xml);
} public void init(String xml)throws XmlException {
try {
if (xml == null ) {
throw new XmlException("xml file is null");
}
xml = xml.trim();
if (xml.equals("")) {
throw new XmlException("xml file is null");
}
SAXReader reader = new SAXReader();
this.xmlDoc = reader.read(new StringReader(xml));
} catch (DocumentException e) {
e.printStackTrace();
log.error("parser xml exception", e);
throw new XmlException(e.getMessage());
}
}
public void initFile(String filePath) throws XmlException{
try {
SAXReader reader = new SAXReader();
//Document document = reader.read(new File(filePath));
//String xml = document.asXML();
//SAXReader reader = new SAXReader();
this.xmlDoc =reader.read(new File(filePath));
// Document document = DocumentHelper.parseText(xml);
} catch (DocumentException e) {
e.printStackTrace();
log.error("parser xml exception", e);
throw new XmlException(e.getMessage());
}
} public Element getRootElement(String xml)throws XmlException {
init(xml);
Element rootElement = xmlDoc.getRootElement();
return rootElement;
} public Document getXmlDoc() {
return xmlDoc;
} public void setXmlDoc(Document xmlDoc) {
this.xmlDoc = xmlDoc;
}
}