我有 xml 字符串文件,我想在 xml 字符串文件的第二行添加 xml-stylesheet。但是下面给出的代码给了我这样的输出:首先是 xml-string 文件,然后在 xml-string 文件的末尾附加 xml-stylesheet 但我想要在我的 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;
}
}