Question

I am linking to a webservice all works fine. Below is my codes.

private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = \n");
        StreamResult result = new StreamResult(System.out);     
        transformer.transform(sourceContent, result);
    }

I am not too sure of the transformerfactory. What I need to do now is to traverse through the results and look for below tag.

<Table diffgr:id="Table1" > and there after there will be few tags in it for e.g.

<rID>1212</rID>
<sNo>15677</sNo>

So what is the best way as some require to covert it into string is that necessary?

Was it helpful?

Solution

Transform to document (unchecked):

      TransformerFactory tf = TransformerFactory.newInstance();  
      Transformer transformer = tf.newTransformer();  
      DOMResult result = new DOMResult();  
      transformer.transform(sourceContent, result);  
      Document doc = (Document) result.getNode(); 

Find in document:

        String tag = "Table";
        String attr = "diffgr:id";
        String attrValue = "Table1";
        NodeList list = doc.getElementsByTagName("Table");
        Element tableNode = null;
        for (int i = 0; i < list.getLength(); i++) {
            tableNode = ((Element) list.item(i));
            String currentAttrValue = tableNode.getAttribute(attr);
            if (attrValue.equals(currentAttrValue)) {
                break;
            }
        }
        String childTag1 = "rID";
        String childTag2 = "sNo";
        Node child1 = (Node) tableNode.getElementsByTagName(childTag1).item(0);
        Node child2 = (Node) tableNode.getElementsByTagName(childTag2).item(0);
        String rIDValue = child1.getTextContent();
        String sNoValue = child1.getTextContent();

OTHER TIPS

You code Transformer transformer = transformerFactory.newTransformer(); is creating an "identity transformer" which copies the input unchanged, so it's not really doing anything useful. What you want here is a real (XSLT) transformer which actually extracts the information you need: something like

<xsl:template match="/">
  <xsl:copy-of select="//Table[@diffgr:id='Table1']"/>
</xsl:template>

which you can compile using transformerFactory.newTemplates().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top