将样式表添加到XML文档中

时间:2013-09-19 09:11:59

标签: java css xml

我想使用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)

请指导我做错了什么。感谢

1 个答案:

答案 0 :(得分:0)