首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在xml字符串的第二行添加xml-样式表。

在xml字符串的第二行添加xml-样式表。
EN

Stack Overflow用户
提问于 2013-10-08 06:38:31
回答 1查看 607关注 0票数 1

如何在xml文件的第二行添加样式表,我已经编写了代码,但它在xml字符串文件的第一行中添加了样式表,请查看我的代码它正在工作,但它在xml字符串的第一行添加样式表,我的要求是在xml字符串的第二行添加xml-样式表,请帮助。

代码语言:javascript
复制
public class StringToDocumentToString {

    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=\"cda.xsl\"");
        Element root = doc.getDocumentElement();
        doc.insertBefore((Node) pi, root);
        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.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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-08 12:11:34

转化前

代码语言:javascript
复制
transformer.transform(new DOMSource(doc), new StreamResult(writer));

尝试使用以下输出属性,

代码语言:javascript
复制
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19240770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档