Discussions

XML & Web services: Problem in transforming a SOAP using DOM

  1. Problem in transforming a SOAP using DOM (11 messages)

    Hi all,
    I am trying to transform an xml file another xml
    applying stylesheet to it. I am using JAXP apis to
    perform this.
    I am able to transform the file except that, the
    result of transformation is in the form <> for
    all tags instead of <>.
    Can you please suggest me how can i get the out put in
    xml format itself?

    Here is part of my code.

    transformer.setOutputProperty("method", "xml");
                      transformer.setOutputProperty("encoding",
    "UTF-8");
                      transformer.setOutputProperty("indent", "yes");
                      transformer.setOutputProperty("media-type",
    "text/xml");
                    transformer.setOutputProperty("standalone", "yes");
            
    transformer.setOutputProperty("omit-xml-declaration",
    "yes");

    OutputStream os = new FileOutputStream("output.xml");
    transformer.transform(xmlDomSource, new
    StreamResult(os));


    Thanks,
    soumya
  2. Problem in transforming a SOAP using DOM[ Go to top ]

    If the indentation is your question, it is certainly not as neat as the carefully hand-coded indentation of the input documents. However, if white space isn't particularly significant in your output document, you can change this and ask the formatter for "pretty printed" XML with the nesting of different elements indicated by the indentation. This is accomplished by the indent attribute of the xsl:output element. If this attribute has the value yes (the default is no), then the processor is allowed (but not required) to insert (but not remove) extra white space into the output to try to "pretty print" the output. This may include indentation and line breaks. For example,

    <xsl:output indent="yes"/>

    You cannot, however, specify how much you want each level indented (for example, by two spaces or one tab). That's up to the formatter. Together, the xsl:strip-space and the indent attribute of the xsl:output element allow you to produce output that's almost as attractive as the most painstakingly hand-crafted XML.

    Try and see if it works with JAXP.

    Cheers.
  3. Problem in transforming a SOAP using DOM[ Go to top ]

    Hi srinivas,
    My problem was in interpretation of xml tags.
    after transforming the xml, i am writing it to an other xml file and in there, it looks like, &lt, &gt ('&lt' '&gt'). I am expecting it to be < >.
  4. Problem in transforming a SOAP using DOM[ Go to top ]

    I'm interested in the answer too.
  5. Guys,

    Need some context to help out, such as what tools are you using, partial code, input and output. Here is an example I tried under J2SE1.5 and it works! I tried with omit-xml-declaration="yes" and omit-xml-declaration="no". Both works great.
    ===
    XSL
    ===
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="xml" encoding="UTF-8" indent="yes" media-type="text/xml" standalone="yes" omit-xml-declaration="no" />

    <xsl:template match="/">
      <portfolio>
       <xsl:apply-templates/>
      </portfolio>
    </xsl:template>

    <xsl:template match="portfolio">
       <table border="2" width="50%">
         <xsl:for-each select="stock">
           <tr>
             <td>
               <xsl:value-of select=
               "symbol"/>

             </td>
           <td>
              <xsl:value-of select="name"/>
            </td>
           <td>
              <xsl:value-of select="price"/>
           </td>

            </tr>
          </xsl:for-each>
        </table>
      </xsl:template>
    </xsl:stylesheet>

    =========
    INPUT XML
    =========
    <?xml version="1.0" encoding="UTF-8"?>
    <portfolio>
      <stock>
        <symbol>SUNW</symbol>
        <name>Sun Microsystems</name>
        <price>17.1</price>
      </stock>
      <stock>
        <symbol>AOL</symbol>
        <name>America Online</name>
        <price>51.05</price>
      </stock>
      <stock>
        <symbol>IBM</symbol>
        <name>International Business Machines</name>
        <price>116.10</price>
      </stock>
      <stock>
        <symbol>MOT</symbol>
        <name>MOTOROLA</name>
        <price>15.20</price>
      </stock>
    </portfolio>

    ==========
    OUTPUT XML
    ==========
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <portfolio>
    <table border="2" width="50%">
    <tr>
    <td>
    SUNW
    </td>
    <td>Sun Microsystems</td>
    <td>17.1</td>
    </tr>
    <tr>
    <td>
    AOL
    </td>
    <td>America Online</td>
    <td>51.05</td>
    </tr>
    <tr>
    <td>
    IBM
    </td>
    <td>International Business Machines</td>
    <td>116.10</td>
    </tr>
    <tr>
    <td>
    MOT
    </td>
    <td>MOTOROLA</td>
    <td>15.20</td>
    </tr>
    </table>
    </portfolio>

    ======
    SOURCE
    ======
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <portfolio>
    <table border="2" width="50%">
    <tr>
    <td>
    SUNW
    </td>
    <td>Sun Microsystems</td>
    <td>17.1</td>
    </tr>
    <tr>
    <td>
    AOL
    </td>
    <td>America Online</td>
    <td>51.05</td>
    </tr>
    <tr>
    <td>
    IBM
    </td>
    <td>International Business Machines</td>
    <td>116.10</td>
    </tr>
    <tr>
    <td>
    MOT
    </td>
    <td>MOTOROLA</td>
    <td>15.20</td>
    </tr>
    </table>
    </portfolio>

    If you feel you cannot disclose your data/sources here, mail me at rajubandi@hotmail.com
  6. The source part was incorrect in the previous message. Here it is.

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerConfigurationException;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;

    public class SimpleTransform {
    public static void main(String[] args) throws TransformerException,
    TransformerConfigurationException, FileNotFoundException,
    IOException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource(
    "c:\\jaxp
    stocks.xsl"));

    transformer.setOutputProperty("method", "xml");
    transformer.setOutputProperty("encoding", "UTF-8");
    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty("media-type", "text/xml");
    transformer.setOutputProperty("standalone", "yes");
    transformer.setOutputProperty("omit-xml-declaration", "no");

    transformer.transform(new StreamSource(args[0]), new StreamResult(
    new FileOutputStream(args[1])));
    System.out.println("** The output is written in " + args[1] + " **");
    }
    }
  7. Hello Srinivas,
    Thanks for the code sample. That works as it is mentioned.
    But i am still running with the problem as my requirement is quite different. Let me explain.

    In my webservice, i receive a SOAP message.

    <SOAP:Envelope>
       <Header>--Authentication Information---</Header>
       <Body>
         <BusinessInfo>
         <Application><moreInfo></moreInfo></Application>
         </BusinessInfo>
       </Body>
    </SOAP:Envelope>

    Soon i receive this message, i need to extract only the contents of the 'Body' and save it in a new xml file to perform futher business operations on it. Hence my output should be something like this.
       <BusinessInfo>
         <Application><moreInfo></moreInfo></Application>
       </BusinessInfo>

    First SOAP message could be considered as a.xml and the output (above) may be in b.xml.
    So its just that i want to remove the whole soap tags and just retain the business info. I will be performing xpath operations etc..on the output.

    Presently i am trying to do this using DOM api's.

    Please sugest me with this.
    Hope my explaination was clear.

    Thanks for the help.

    -soumya
  8. Is the original problem, occurence of '<' not occuring any more?

    The DOM does make sense if you need to form tree for further processing.

    Yes. Using XSLT, you can prune the tree and work off of the tree for further processing. The performance of this may be not be beter than forming the DOM tree only once. So if you have a large SOAP message this has to be considered.

    If need further info regarding the xsl, contact me at the e-mail in my previous message.

    Thanks
    Srinivas
  9. Yes the original problem of ('&lt' '&gt') still exist if i have CDATA part in the SOAP body before the business application elements starts. If i remove CDATA, transformation works but the output is just the values of the nodes. I need all the nodes inside the SOAP body with the values.(as explaind in my previous message).

    The bottom line is i want extract the SOAP Body contents and store it in an XML file and also construct a DOM tree out of it so that i can perform some operations on specific nodes.

    Please gimme ur email id. couldn't find it in here.

    thanks,

    -soumya
  10. Using XSLT, you can also perform operations, but it gets little complicated and further difficult to implement application logic.

    Thanks,
    Srinivas
    rajubandi@hotmail.com
  11. In that cse, can you please send me a code sample like your previous sample, with which i can transform the SOAP message and get the SOAP body part(including the elements, elements+values).
    Or if you know any better methods to achieve my goal, you could please suggest.

    Thanks,
    Soumya
  12. Yes. There are several possibilities using XSLT, or DOM Parser or using an XML Binding tool. Can we take this offline ?

    Thanks
    Srinivas
    rajubandi@hotmail.com