将样式表添加到XML文档中(Adding Stylesheet into XML document)

编程入门 行业动态 更新时间:2024-10-27 23:24:19
样式表添加到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.

更多推荐

本文发布于:2023-08-06 21:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455381.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样式表   文档   XML   document   Adding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!