Java 在第二行的现有xml文件中添加xml样式表

Java 在第二行的现有xml文件中添加xml样式表,java,xml,xslt,Java,Xml,Xslt,我有一个xml字符串文件,我想在xml字符串文件的第二行添加xml样式表。 但下面给出的代码给出了如下输出: 首先是xml-string文件,然后在xml-string文件的末尾附加xml样式表,但我希望样式表位于xml-string的第二行。请建议我如何执行此操作。 谢谢 我的代码是: public class StringToDocumentToString { public static void main(String[] args) throws Transforme

我有一个xml字符串文件,我想在xml字符串文件的第二行添加xml样式表。 但下面给出的代码给出了如下输出: 首先是xml-string文件,然后在xml-string文件的末尾附加xml样式表,但我希望样式表位于xml-string的第二行。请建议我如何执行此操作。 谢谢

我的代码是:

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=\"my.stylesheet.xsl\"");
    Element root = doc.createElement("root-element");
    doc.appendChild(root);

    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;
}
}
公共类StringToDocumentToString{
公共静态void main(字符串[]args)
引发TransformerConfiguration异常{
字符串xmlstring=null;
String filepath=“E:/C-CDA/MU2\u CDA\u WORKSPACE/AddingStylesheetTOxml/documentfile.txt”;
最终字符串xmlStr=“\n”
+“Pankaj25\n”
+“developerMail”;
文档doc2=转换字符串到文档(xmlStr);
文档doc1=null;
试一试{
doc1=添加样式表(doc2);
}捕获(ParserConfiguration异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
字符串str=convertDocumentToString(doc1);
系统输出打印项次(str);
}
私有静态文档添加样式表(
文档文档)引发TransformerConfiguration异常,
ParserConfiguration异常{
ProcessingInstructionImpl pi=(ProcessingInstructionImpl)文档
.createProcessingInstruction(“xml样式表”,
“type=\”text/xsl\”href=\”my.stylesheet.xsl\”;
元素根=doc.createElement(“根元素”);
doc.appendChild(根);
doc.insertBefore((节点)pi,root);
退货单;
}
私有静态字符串convertDocumentToString(文档文档){
TransformerFactory tf=TransformerFactory.newInstance();
变压器;
试一试{
变压器=tf.新变压器();
//下面是删除XML声明的代码
//setOutputProperty(OutputKeys.OMIT_XML_声明,
//“是”);
StringWriter编写器=新的StringWriter();
transform(新的DOMSource(doc)、新的StreamResult(writer));
字符串输出=writer.getBuffer().toString();
返回输出;
}捕获(转换异常e){
e、 printStackTrace();
}
返回null;
}
私有静态文档convertStringToDocument(字符串xmlStr){
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
文档生成器;
单据单据=空;
试一试{
builder=factory.newDocumentBuilder();
doc=builder.parse(新的InputSource(新的StringReader(xmlStr));
}捕获(例外e){
e、 printStackTrace();
}
退货单;
}
}

语句
Element root=doc.createElement(“根元素”)创建一个名为
根元素的新元素,当您调用
doc.appendChild(root)时实际上是将其附加到文档末尾。在这两条语句之后,您的文档将是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<Emp id="1">
    <name>Pankaj</name>
    <age>25</age>
    <role>Developer</role>
    <gen>Male</gen>
</Emp>
<root-element/>
Element root = doc.getDocumentElement();
doc.insertBefore((Node) pi, root);