在Java中解析XML字符串的最佳方法?

问题描述:

我使用javax.xml.parsers.DocumentBuilder解析Java中的字符串。然而,没有直接解析字符串的函数,所以我不是这样做:在Java中解析XML字符串的最佳方法?

public static Document parseText(String zText) { 
    try 
    { 
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
     Document doc = dBuilder.parse(new InputSource(new StringReader(zText))); 
     doc.getDocumentElement().normalize(); 
     return doc; 
    } 
    catch (Exception e) { 
      e.printStackTrace(); 
    } 
    return null; 
} 

这是做的最好的方法是什么?我觉得必须有一个更简单的方式......谢谢!

直接回答你的问题 - 据我所知,没有更好的方法。使用输入源是因为它更通用并且可以处理来自文件的输入,字符串或跨线是我的理解。

您也可以尝试使用SAX Xml解析器 - 它稍微更基本一些,并使用访问者模式,但它可以完成工作,而对于小型数据集和简单的XML模式来说,它非常易于使用。 SAX也包含在核心JRE中。

我个人比较喜欢dom4j。看看他们的快速入门,这很简单。

如果我很匆忙,或者我不在乎,我就不会正常化。当你需要的时候你可以规范化节点。

我同意aperkins这里是我的javax帮手:

/** 
* Returns a {@code Document} from the specified XML {@code String}. 
* 
* @param xmlDocumentString a well-formed XML {@code String} 
* @return a {@code org.w3c.dom.Document} 
*/ 
public static Document getDomDocument(String xmlDocumentString) 
{ 
    if(StringUtility.isNullOrEmpty(xmlDocumentString)) return null; 

    InputStream s = null; 

    try 
    { 
     s = new ByteArrayInputStream(xmlDocumentString.getBytes("UTF-8")); 
    } 
    catch(UnsupportedEncodingException e) 
    { 
     throw new RuntimeException("UnsupportedEncodingException: " + e.getMessage()); 
    } 

    return XmlDomUtility.getDomDocument(s); 
} 

这个助手依赖于另一个问题:

/** 
* Returns a {@code Document} from the specified {@code InputStream}. 
* 
* @param input the {@code java.io.InputStream} 
* @return a {@code org.w3c.dom.Document} 
*/ 
public static Document getDomDocument(InputStream input) 
{ 
    Document document = null; 
    try 
    { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     factory.setNamespaceAware(true); 
     DocumentBuilder builder = factory.newDocumentBuilder(); 
     document = builder.parse(input); 
    } 
    catch(ParserConfigurationException e) 
    { 
     throw new RuntimeException("ParserConfigurationException: " + e.getMessage()); 
    } 
    catch(SAXException e) 
    { 
     throw new RuntimeException("SAXException: " + e.getMessage()); 
    } 
    catch(IOException e) 
    { 
     throw new RuntimeException("IOException: " + e.getMessage()); 
    } 

    return document; 
} 

更新:这是我进口:

import java.io.ByteArrayInputStream; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.UnsupportedEncodingException; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.xml.sax.SAXException; 
+0

Rasx:哪里StringUtility和XmlDomUtility进口? – 2009-10-06 03:25:31

+0

我正在使用标准的JavaSE javax库: import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; – rasx 2009-10-06 18:28:57

您可以尝试的另一种选择是Castor,我认为这使事情变得如此简单得多:

http://www.castor.org/