2011-11-24 60 views
8

我正在使用this鏈接使用DOM生成XML文件。它說「Xerces解析器與JDK 1.5發行版捆綁在一起,因此您無需單獨下載解析器。」使用Xerces將DOM序列化到FileOutputStream

但是,當我在Eclipse Helios中編寫以下代碼行時,即使我的系統中安裝了Java 1.6,也會出現編譯時錯誤。

import org.apache.xml.serialize.XMLSerializer; 

這是爲什麼?

回答

26

Xerces確實與JDK捆綁在一起,但您應該在javax.xml.parsers下將其與JAXP API結合使用。檢查下面程序的輸出。

另外,要序列化XML Document,您應該使用DOM Level 3 Load和Save(存在於JDK中)或者沒有樣式表(標識轉換)的XSLT轉換。其餘依賴於具體的實施。 Xerces XMLSerializer已棄用:

已棄用。這個類在Xerces 2.9.0中被棄用。建議新應用程序使用DOM Level 3 LSSerializer或JAXP的XML轉換API(TrAX)來序列化XML。有關更多信息,請參閱Xerces文檔。

下面是序列化與DOM Level 3的一個例子:

import org.w3c.dom.*; 
import org.w3c.dom.bootstrap.DOMImplementationRegistry; 
import org.w3c.dom.ls.*; 

public class DOMExample3 { 

    public static void main(String[] args) throws Exception { 
     DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();  
     DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0"); 
     if (impl == null) { 
      System.out.println("No DOMImplementation found !"); 
      System.exit(0); 
     } 

     System.out.printf("DOMImplementationLS: %s\n", impl.getClass().getName()); 

     LSParser parser = impl.createLSParser(
       DOMImplementationLS.MODE_SYNCHRONOUS, 
       "http://www.w3.org/TR/REC-xml"); 
     // http://www.w3.org/2001/XMLSchema 
     System.out.printf("LSParser: %s\n", parser.getClass().getName()); 

     if (args.length == 0) { 
      System.exit(0); 
     } 

     Document doc = parser.parseURI(args[0]); 

     LSSerializer serializer = impl.createLSSerializer(); 
     LSOutput output = impl.createLSOutput(); 
     output.setEncoding("UTF-8"); 
     output.setByteStream(System.out); 
     serializer.write(doc, output); 
     System.out.println(); 
    } 
} 

這裏是恆等變換的例子:

import org.w3c.dom.Document; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.transform.OutputKeys; 
import javax.xml.transform.Result; 
import javax.xml.transform.Source; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 
import javax.xml.transform.stream.StreamSource; 

public class DOMExample2 { 
    public static void main(String[] args) throws Exception { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder parser = factory.newDocumentBuilder(); 

     System.out.println("Parsing XML document..."); 
     Document doc; 
     doc = parser.parse(args[0]); 

     // Xerces Java 2 

     /* Deprecated. This class was deprecated in Xerces 2.9.0. 
     * It is recommended that new applications use the DOM Level 3 
     * LSSerializer or JAXP's Transformation API for XML (TrAX) 
     * for serializing XML and HTML. 
     * See the Xerces documentation for more information. 
     */ 
     /* 
     System.out.println("XERCES: Displaying XML document..."); 
     OutputFormat of = new OutputFormat(doc, "ISO-8859-1", true); 
     PrintWriter pw = new PrintWriter(System.out); 
     BaseMarkupSerializer bms = new XMLSerializer(pw, of); 
     bms.serialize(doc); 
*/ 
     // JAXP 

     System.out.println("JAXP: Displaying XML document..."); 
     TransformerFactory transFactory = TransformerFactory.newInstance(); 
     System.out.println(transFactory.getClass().getName()); 
     //transFactory.setAttribute("indent-number", 2); 
     Transformer idTransform = transFactory.newTransformer(); 
     idTransform.setOutputProperty(OutputKeys.METHOD, "xml"); 
     idTransform.setOutputProperty(OutputKeys.INDENT,"yes"); 
     // Apache default indentation is 0 
     idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");     
     Source input = new DOMSource(doc); 
     Result output = new StreamResult(System.out); 
     idTransform.transform(input, output); 
    } 
} 
+0

謝謝。這有助於:) – whitehat

+0

如果你認爲它可能被認爲是一個答案,你能標記它嗎? – lkuty

+1

我點擊「這個答案很有用」,我增加了計數:) :) – whitehat

1

它將在,IIRC,com.sun.org.apache.xml.serialize.XMLSerializer。但是,這些都是私人課程,可能會隨時更改。我建議使用標準的公共API(javax.*和朋友)。 (使用不帶任何XSLT的轉換API。)