java - 如何使用 Java 将 xml 样式表包含到 XML 文件中

标签 java xml styles

我想使用 Java 将 xml 样式表添加到 XML 文件的第二行。我在堆栈溢出中找到了几乎这样的帖子:Add xml-stylesheet and get standalone = yes ,但它会在我的 xml 文件末尾附加样式表。

我可以在 XML 文件的第二行包含(添加)吗?请推荐我。

最佳答案

这里是在 xml 字符串的第二行添加 xml-stylesheet 的完整测试代码。您只需复制代码并运行即可。

public static void main(String[] args)
        throws TransformerConfigurationException {
    String xmlstring = null;
    String filepath = "E:/C-CDA/MU2_CDA_WORKSPACE/AddingStylesheetTOxml/documentfile.txt";
    final String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
            + "<Emp id=\"1\"><name>Pankaj</name><age>25</age>\n"
            + "<role>Developer</role><gen>Male</gen></Emp>";
    Document doc2 = convertStringToDocument(xmlStr);
    Document doc1 = null;
    try {
        doc1 = addingStylesheet(doc2);
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String str = convertDocumentToString(doc1);
    System.out.println(str);
}

private static <ProcessingInstructionImpl> Document addingStylesheet(
        Document doc) throws TransformerConfigurationException,
        ParserConfigurationException {
    ProcessingInstructionImpl pi = (ProcessingInstructionImpl) doc
            .createProcessingInstruction("xml-stylesheet",
                    "type=\"text/xsl\" href=\"mystylesheet.xsl\"");
    Element root = doc.getDocumentElement();
    doc.insertBefore((Node) pi, root);



    //trans.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(bout, "utf-8")));
    return doc;

}

private static String convertDocumentToString(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = tf.newTransformer();
        // below code to remove XML declaration
        // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
        // "yes");
        StringWriter writer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return null;
}

private static Document convertStringToDocument(String xmlStr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    Document doc = null;
    try {
        builder = factory.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(xmlStr)));

    } catch (Exception e) {
        e.printStackTrace();
    }
    return doc;
}

关于java - 如何使用 Java 将 xml 样式表包含到 XML 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19154976/

相关文章:

Java HashMap<Integer, Double> 插入 NaN

xml - XPath。 Child 有一个默认的命名空间,如何获取它的元素?

silverlight - 在 Silverlight 5 中合并 ResourceDictonaries 中使用样式

css 从 svg 样式溢出到另一个

java - 如何使用扫描仪扫描浮点值?

java - 在 JUnit 测试用例中找不到测试方法

java - 从 org.w3c.dom.Node 获取 Xpath

xml - XSLT:我可以使用 xslt 更新 xml 节点中的值吗?

html - CSS 两层渐变效果

java - 将图像添加到用户单击布局的位置