JAVA 解析XML 的时候不知道XML 文件名。只知道我通过 HTTP接收到的XML内容。 该如何解析呢 ?网上的例子文档好像都是通过文件名读取XML文件 ,然后再 用SAX。     

解决方案 »

  1.   

    dom4j Document doc = null;
    SAXReader reader = new SAXReader();
    doc = reader.read(new ByteArrayInputStream(内容.getBytes()));后面就是正常解析了
      

  2.   

    提供一种简单的方法:                String url = "http://xxx"
    SAXReader saxReader = new SAXReader();
    org.dom4j.Document document = saxReader.read(url);
    String result = document.getRootElement().elemen("RESULT").getTextTrim();//获得根节点RESULT 的内容
      

  3.   

    使用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);
      

  4.   

    文件名只是用来定位xml的,如果直接用流的话就可以不用知道文件名了
      

  5.   

    doc = reader.read(new ByteArrayInputStream(内容.getBytes())); 好像有问题。
      

  6.   

    package com.ilongman.acs.base;import java.io.File;
    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;
    }
    }