我有一個將在Java應用程序中處理的XML文檔。 但是,我需要使用XSLT文件對其進行轉換,以便以後可以進行處理。在處理之前用XSLT轉換XML文檔
這就是我現在如何加載XML文件。
DocumentBuilderFactory factory;
DocumentBuilder docbuilder;
Document doc;
Element root;
factory = DocumentBuilderFactory.newInstance();
try
{
// open up the xml document
docbuilder = factory.newDocumentBuilder();
doc = docbuilder.parse(new FileInputStream(m_strFileName));
// get the document type
doctype = doc.getDoctype();
strDTD = doctype.getPublicId();
// get the root of the document
root = doc.getDocumentElement();
// get the list of child nodes
nodes = root.getChildNodes();
// now process each node
...
}
catch(ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
如何將XSLT轉換應用於XML文檔,然後獲取新文檔的根節點?
請注意,我是而不是想要將生成的xml樹寫入磁盤。
謝謝,這很有幫助。我是否理解正確,產生的文檔現在存儲在'doc'中? (多麼奇怪的API ......) – dokaspar
@dokaspar - 是的,你說得很對,最終的文檔確實保存在變量「doc」中。 – Simon