2009-10-05 56 views
4

我使用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; 
} 

這是做的最好的方法是什麼?我覺得必須有一個更簡單的方式......謝謝!

回答

5

直接回答你的問題 - 據我所知,沒有更好的方法。使用輸入源是因爲它更通用並且可以處理來自文件的輸入,字符串或跨線是我的理解。

您也可以嘗試使用SAX Xml解析器 - 它稍微更基本一些,並使用訪問者模式,但它可以完成工作,而對於小型數據集和簡單的XML模式來說,它非常易於使用。 SAX也包含在覈心JRE中。

2

我個人比較喜歡dom4j。看看他們的快速入門,這很簡單。

1

如果我很匆忙,或者我不在乎,我就不會正常化。當你需要的時候你可以規範化節點。

1

我同意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