7 Replies Latest reply on May 3, 2011 2:44 AM by serkan.s.eskici.online.nl

    How to easily display Indented XML files

    slashfr

      HI,
      I Generated a web application from two MySQL tables (Tracking and Error), using the SEAM code generation.
      In my tables, i have a BLOB column that stores XML files in binary format.
      I want to display the content of these files on my XHTML pages INDENTED.
      i didn't find any solution, the only solution i found is to transform my byte array into a String in the EJB, using the following java instruction :
      messageBody is a byte array


      String xmlUnformatted =  new String( messageBody , "Cp1252" );
      But the string is not indented :(


      Thank you for your help

        • 1. Re: How to easily display Indented XML files
          slashfr

          No Idea?

          • 2. Re: How to easily display Indented XML files
            ctomc

            Maybe this will help:



            public static String getXML(Document document, boolean indent) {
            
                      OutputFormat format;
                      StringWriter stringOut;
                      XML11Serializer serial;
                      if (document.getOwnerDocument() == null) {
                           format = new OutputFormat(org.apache.xml.serialize.Method.XML, null, true);
                      } else {
                           format = new OutputFormat(document.getOwnerDocument(), null, true);
                      }
                      format.setOmitXMLDeclaration(true);
                      if (indent) {
                           format.setIndent(2);
                           format.setLineSeparator("\r\n");
                      } else {
                           format.setIndent(0);
                           format.setLineSeparator("");
                      }
                      stringOut = new StringWriter();
                      serial = new XML11Serializer(stringOut, format);
                      serial.setNamespaces(true);
            
                      try {
                           
                           serial.serialize(document);
                           
                           return (stringOut.toString());
                      } catch (java.io.IOException ioe) {
                           throw new RuntimeException("Strange error. IOException occurred where it cannot!", ioe);
                      }
                 }
            




            cheers,
            tomaz

            • 3. Re: How to easily display Indented XML files

              ... or using XSLT. Look here: Oliver's XSLT page

              • 4. Re: How to easily display Indented XML files
                nicolas.bielza

                Several approaches for indenting XML documents are discussed here.


                I'm using com.sun.org.apache.xml.internal.serialize.XMLSerializer and it works fine for me (although it's undocumented and non portable).

                • 5. Re: How to easily display Indented XML files
                  nicolas.bielza

                  I meant to say com.sun.org.apache.xml.internal.serialize.OutputFormat works fine for me !



                    public String format(Document document, int indent) throws IOException {
                      OutputFormat format = new OutputFormat(document);
                      format.setIndenting(true);
                      format.setIndent(indent);
                      StringWriter stringWriter = new StringWriter();
                      Writer output = new BufferedWriter( stringWriter );
                      XMLSerializer serializer = new XMLSerializer(output, format);
                      serializer.serialize(document);
                      return stringWriter.toString();
                    }
                  


                  • 6. Re: How to easily display Indented XML files
                    edmentay

                    hi,
                    I had tried the solution above. but its did't work!! my xhtml page is,

                    <h:outputFormat id="xmlout" value="#{configList.xmlDoc}" rendered="true" />

                    backbean statement is,

                    ByteArrayOutputStream output = new ByteArrayOutputStream();
                    output.write(tempapp.getxmlbytes());
                                   
                    byte b[] = output.toByteArray();
                                   
                    db = dbf.newDocumentBuilder();
                                           
                    InputSource is = new InputSource();
                    String content = new String(b);

                    is.setCharacterStream(new StringReader(content));

                    this.selectedDocument = db.parse(is);

                    return this.selectedDocument

                    Please help!!

                    Regards,
                    Edmen

                    • 7. Re: How to easily display Indented XML files
                      serkan.s.eskici.online.nl

                      I've the same usecase and what I've done is the following:


                      <s:formattedText value="embrace you string within a singlequote (shows the string as a <code>, cant use it on this wiki message) #{myBean.blobText}" escape="false" />
                      



                      Assuming that the XML blob is already saved with correct indenting in the database.