11 Replies Latest reply: May 19, 2011 1:26 AM by Suneesh Raman-Oracle RSS

    XML Document insertBefore method

    Suneesh Raman-Oracle
      Hi
      I want to create some comments before the root element Can anybody help with some sample code

      <!- -comments here -->
      <xliff xlms="something" version="1.1">
      </xliff>



      Code below is not working ;The comments are always coming at the bottom of the document. I need it just before the root element . Seems some issue with insertBefore method.


                Document doc = dImp.createDocument(XLIFFConstants.NS_URI, XLIFFConstants.ROOT_ELEMENT, null);
                doc.getDocumentElement().setAttribute(XLIFFConstants.ROOT_ATTRIB, XLIFFConstants.ROOT_ATTRIB_VALUE);
                doc.getDocumentElement().setAttribute("xmlns", XLIFFConstants.NS_URI);
                doc.getDocumentElement().appendChild(getNewLineTextNode(doc));
                
                Node headerComment = doc.createComment(XLIFFConstants.HEADER_COMMENT);
                doc.insertBefore(headerComment, doc.getDocumentElement());
                

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd 'at' HH:mm:ss z ");
                Node timeStampComment = doc.createComment(XLIFFConstants.TIMESTAMP_COMMENT_PREFIX +
                                                                            formatter.format(timeStamp));
                doc.insertBefore(timeStampComment, doc.getDocumentElement());


      Thanks
      Suneesh
        • 1. Re: XML Document insertBefore method
          857597
          You know that valid xml can't have more than one root element right? That includes comments.
          • 2. Re: XML Document insertBefore method
            Suneesh Raman-Oracle
            This works well in a standalone application . But does not work when deployed to weblogic. The same code was working fine in Oracle Application servers. Feel some changes in the implementation of insertBefore method in weblogic jar files . Any idea ?
            • 3. Re: XML Document insertBefore method
              EJP
              The idea is that you are attempting to produce invalid XML, and one of the underlying libraries is doing it differently from what you expect. But you don't have any reason to expect any specific behaviour, as the desired result is invald. Solution: don't do it. Put the comments inside the document element for example.
              • 4. Re: XML Document insertBefore method
                Suneesh Raman-Oracle
                So that mean the comment before the root element is not possible through these apis ? Is there any other way to acheive this ?
                • 5. Re: XML Document insertBefore method
                  jduprez
                  So that mean the comment before the root element is not possible through these apis ? Is there any other way to acheive this ?
                  One way is to post-process the XML string to add them "outside of the XML APIs": serialize the XML document to a String, then add the "comment String":
                  docString = "<!-- my comment -->\n" + docString;
                  You should only do that at the very last stage of generating the XML output: if you need further manipulation, you would need to re-parse the XML string into a new Document instance, but that would likely fail because the API of the same environment would reject the invalid XML.

                  But really, have you read the replies about this being invalid (as per XML standard, or at least that's what I read here, I'm not expert)?
                  That means that whoever is consuming the generated XML document you produce may (and should) have trouble using it (they would have to configure their parsers, or whatever exploits the document, to ignore the invalid section, and that is not even always possible, depending on what they use to process the file, or string).

                  J.

                  Edited by: jduprez on May 17, 2011 11:55 AM
                  • 6. Re: XML Document insertBefore method
                    643310
                    Suneesh Raman wrote:
                    So that mean the comment before the root element is not possible through these apis ?
                    It means that invalid XML are not possible through these APIs.
                    • 7. Re: XML Document insertBefore method
                      Suneesh Raman-Oracle
                      This is a standard format of resource bundle file we are generating for the customer. They will look at this header comments and do the post processing ,versioning etc.This application is running for last couple of years in Oracle Application Server . Migrating to weblogic created this issue currently.
                      • 8. Re: XML Document insertBefore method
                        EJP
                        This is a standard format of resource bundle file
                        It is a non-standard and invalid format of resource bundle. You don't need the comments to be in that particular place and you have just run across a configuration in which your 'standard format' is unimplementable.

                        I suggest you take the hint.
                        They will look at this header comments and do the post processing ,versioning etc.
                        Assuming their libraries can handle the invalid XML format. This is another point at which your non-standard standard can break.
                        Migrating to weblogic created this issue currently.
                        Your other alternative is not to migrate to WebLogic.
                        • 9. Re: XML Document insertBefore method
                          Suneesh Raman-Oracle
                          Refering to this document ;
                          http://download.oracle.com/javaee/1.4/tutorial/doc/IntroXML3.html
                          It says comments are ignored by parsers.
                          We want the xml generated something like this

                          <?xml version='1.0' encoding='utf-8'?>

                          <!-- A SAMPLE set of slides -->

                          <slideshow>

                          </slideshow>

                          Dont know why its treating as invalid and adding the comments at the end by the parser.
                          • 10. Re: XML Document insertBefore method
                            DrClap
                            Suneesh Raman wrote:
                            We want the xml generated something like this

                            <?xml version='1.0' encoding='utf-8'?>

                            <!-- A SAMPLE set of slides -->

                            <slideshow>

                            </slideshow>

                            Dont know why its treating as invalid and adding the comments at the end by the parser.
                            If you look at the XML Recommendation, it says that a document consists of a prolog followed by an element. And then (production 22) it says that a prolog consists of an optional XML declaration followed by zero or more "Misc" entries. And (production 27) a "Misc" entry is a comment or a processing-instruction or a white-space character.

                            Which means that what you posted is a well-formed XML document. Comments can indeed appear between the XML declaration and the root element.
                            • 11. Re: XML Document insertBefore method
                              Suneesh Raman-Oracle
                              Then why the API fails while deploying in Weblogic. When I use the set of code :

                              DocumentBuilder builder = factory.newDocumentBuilder();
                              DOMImplementation dImp = builder.getDOMImplementation();

                              Document doc = dImp.createDocument(XLIFFConstants.NS_URI, XLIFFConstants.ROOT_ELEMENT, null);
                              doc.getDocumentElement().setAttribute(XLIFFConstants.ROOT_ATTRIB, XLIFFConstants.ROOT_ATTRIB_VALUE);
                              doc.getDocumentElement().setAttribute("xmlns", XLIFFConstants.NS_URI);
                              doc.getDocumentElement().appendChild(getNewLineTextNode(doc));

                              Node headerComment = doc.createComment(XLIFFConstants.HEADER_COMMENT);
                              doc.insertBefore(headerComment, doc.getDocumentElement());

                              SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd 'at' HH:mm:ss z ");
                              Node timeStampComment = doc.createComment(XLIFFConstants.TIMESTAMP_COMMENT_PREFIX + formatter.format(timeStamp));
                              doc.insertBefore(timeStampComment, doc.getDocumentElement());

                              OutputFormat format = new OutputFormat(doc);
                              format.setPreserveSpace(true);
                              format.setIndenting(true);
                              format.setLineWidth(0);

                              StringWriter writer = new StringWriter();
                              XMLSerializer serializer = new XMLSerializer(writer, format);
                              try {
                              serializer.serialize(doc);
                              // System.out.println(writer.getBuffer().toString());
                              System.out.println(writer.getBuffer().toString();

                              } catch (IOException e) {
                              e.printStackTrace();
                              }


                              I am getting the comment at the bottom of document something like :

                              <?xml version='1.0' encoding='utf-8'?>

                              <slideshow>

                              </slideshow>
                              <!-- A SAMPLE set of slides -->