将样式表添加到XML文档中(Adding Stylesheet into XML document)
我想使用JAVA动态地将样式表添加到XML文档中,但是我得到了一个例外。 我的代码片段如下所示:
final String xmlStr = "My XML tags in form of String here"; Document doc = convertStringToDocument(xmlStr); String documen = addStylesheet( doc); System.out.println(documen); public static String addStylesheet(Document document) { ProcessingInstruction pi = (ProcessingInstruction) document.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"my.stylesheet.xsl\""); document.appendChild((Node) pi); return document.toString(); }抛出的异常是:
Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ProcessingInstructionImpl cannot be cast to javax.xml.stream.events.ProcessingInstruction at StringToDocumentToString.insertStylesheet(StringToDocumentToString.java:70) at StringToDocumentToString.main(StringToDocumentToString.java:27)请指导我做错了什么。 谢谢
I want to add style sheet into XML document dynamically using JAVA, However i am getting an exception. My code snippet looks like:
final String xmlStr = "My XML tags in form of String here"; Document doc = convertStringToDocument(xmlStr); String documen = addStylesheet( doc); System.out.println(documen); public static String addStylesheet(Document document) { ProcessingInstruction pi = (ProcessingInstruction) document.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"my.stylesheet.xsl\""); document.appendChild((Node) pi); return document.toString(); }Exception thrown is:
Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ProcessingInstructionImpl cannot be cast to javax.xml.stream.events.ProcessingInstruction at StringToDocumentToString.insertStylesheet(StringToDocumentToString.java:70) at StringToDocumentToString.main(StringToDocumentToString.java:27)Please guide me what I am doing wrong. thanks
最满意答案
看看: http : //xerces.apache.org/xerces-j/apiDocs/org/apache/xerces/dom/ProcessingInstructionImpl.html
似乎除了Java之外还有一个ProcessingInstruction接口。
Take a look at : http://xerces.apache.org/xerces-j/apiDocs/org/apache/xerces/dom/ProcessingInstructionImpl.html
It seems that there is a ProcessingInstruction interface, other than Java's.
更多推荐
发布评论