Infolinks In Text Ads

Adding XML-Stylesheet at the second line of XML Document In Java

How to Add xml-stylesheet at the second line of xml string.
I have an xml string document in which i want to add xml style sheet at the second line of the document.
  • First i have converted my xml string document to document
  • then adding style-sheet at the second line of xml document 
  • and then converted to string xml document.

The main method  is :
public static void main(String[] args)
                  throws TransformerConfigurationException {
            final String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
                        + "<Emp id=\"1\"><name>aurangzeb</name><age>25</age>\n"
                        + "<role>Developer</role><gen>Male</gen></Emp>";
            Document beforeAddingStyleSheetDocument = convertStringToDocument(xmlStr);
            Document afterAddingStyleSheetDocument = null;
            try {
                  afterAddingStyleSheetDocument = addingStylesheet(beforeAddingStyleSheetDocument);
            } catch (ParserConfigurationException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
            String stringAfterAddingStylesheet = convertDocumentToString(afterAddingStyleSheetDocument);
            System.out.println(stringAfterAddingStylesheet);
      }


 First i have converted my xml string document to document

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;
      }

 Then adding style-sheet at the second line of xml document.
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);
           
     
           
            //trans.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(bout, "utf-8")));
            return doc;

      }

 and then converted to string xml document.

      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;
      }

   transformer.setOutputProperty(OutputKeys.INDENT, "yes"); this line of code  placed the xml-stylesheet document at the second line of xml Documnet.

The output  of the above code is :

Newer Post Older Post

Leave a Reply

Powered by Blogger.