2012-01-16 80 views

回答

2

嘗試薩克斯接口

private String xmlURL = "http://mydomain.com/xml/extract-2000.xml"; 

XMLReader reader = XMLReaderFactory.createXMLReader(); 
reader.setContentHandler(handler); 
reader.parse(new InputSource(new URL(xmlURL).openStream())); 

欲瞭解更多信息,關於SAX檢查this link

+0

嗨,我使用SAX,但不幸的是,我需要加載將XML轉換爲字符串並首先對其進行處理。 – 2012-01-16 17:09:38

2

檢查這個代碼:

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
    InputStream inputStream = new FileInputStream(new File("http://mydomain.com/xml/extract-2000.xml")); 
    org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream); 
    StringWriter stw = new StringWriter(); 
    Transformer serializer = TransformerFactory.newInstance().newTransformer(); 
    serializer.transform(new DOMSource(doc), new StreamResult(stw)); 
    stw.toString(); 
+0

+1用於變壓器。我建議使用StreamSource而不是DOMSource來提高性能。 – 2012-01-16 12:31:23

相關問題