我使用Java和XSL樣式表從XML文件檢索值並將其輸出到文本文件。javax.xml.transform.Source的字符串操作
下面是所使用的程序:
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("transform.xsl"));
Transformer transformer = factory.newTransformer(xslt);
Source text = new StreamSource(new File("inputXML.txt"));
transformer.transform(text, new StreamResult(new File("output.txt"))) ;
但最近我發現XML文件,我會閱讀將有2個節點,而不是一個。所以我想這樣做的字符串操作來添加自己的根節點編程,使我能避免以下錯誤:
ERROR: 'The markup in the document following the root element must be well-formed.' ERROR: 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: The markup in the document following the root element must be well-formed.'
不過,我不能做任何字符串操作對javax.xml.transform.Source (鑄造不工作)。 我不想使用中間文件來添加我的根節點,因爲我擔心它會證明代價高昂,因爲我需要處理接近50k的XML記錄。
我對如何處理「輸入」以實現結果略有差異。謝謝 。非常感激 –