Solved

How to create XML Document from WSDL File?

Posted on 2006-06-12
12
510 Views
Last Modified: 2013-11-19
I am using Java programming Language. I have WSDL file and DTD file. I am not trying to invoke Web serviceses. All I want to write java program that takes WSDL file as parameter and parse it and create XML Document using DOM? Any help will be appreciated.

Thanks in advance

Tejas
0
Comment
Question by:T_Shah
  • 6
  • 6
12 Comments
 
LVL 15

Accepted Solution

by:
bpmurray earned 250 total points
ID: 16888488
The WSDL is already an XML document. Or do you mean that you want to load the WSDL as an in-memory DOM? If the latter, here's a very simple skeleton:

package my.parser;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DOMLoader {

          // Global value so it can be ref'd by the tree-adapter
          static Document document;

          public static void main(String argv[])
          {
              if (argv.length != 1) {
                  System.err.println("No file specified.");
                  System.exit(1);
              }

              File fil = new File(argv[0]);
              if (null == fil) {
                    System.err.println("Can't open file " + argv[0]);
                    System.exit(1);
              }
              
              DocumentBuilderFactory builderFactory =  DocumentBuilderFactory.newInstance();
              
              // builderFactory.setValidating(true); // Do you need validation?
              builderFactory.setNamespaceAware(true); // Need namespaces for WSDL

              try {
                 DocumentBuilder domBuilder = builderFactory.newDocumentBuilder();
                 document = domBuilder.parse(fil);
                 dumpDOM(document);
              } catch (Exception e) { // Error - dump the stack
                    e.printStackTrace();
              }
          } // main


          // The DOM node types from the W3C Level 2 Core Spec
            static final String[] nodeTypeName = { "UNKNOWN", "ELEMENT", "ATTR",
                     "TEXT", "CDATA", "ENTITYREF",
                     "ENTITY","PROCINSTR", "COMMENT",
                     "DOCUMENT", "DOCTYPE", "DOCFRAGMENT",
                     "NOTATION" };

            private static void dumpDOM(Node node) {
                  NodeList nodes = node.getChildNodes();
                  int count = nodes.getLength();
                  for (int iX=0; iX<count; iX++) {
                        Node child = nodes.item(iX);
                        System.out.println(nodeTypeName[child.getNodeType()] ); // process the node
                        dumpDOM(child); // process children too
                  }      
            }
          
      }
0
 

Author Comment

by:T_Shah
ID: 16896449
Thanks a lot for your answer. It helped me a lot.
Yes!! You are right. I want to load the WSDL as an in-memory DOM and then create another XML file dynamically with selected attributes of an element.
For now I would like to create XML file like this way.
<complexType>
      <element>
      <element>
      <element>
</complexType>

I have following type of WSDL file:

      <xsd:complexType name="Info">
            <xsd:all>
                  <xsd:element name="ID" type="xsd:string" nillable="false"/>
            </xsd:all>
      </xsd:complexType>

        <wsdl:message name="GetWeb">
            <wsdl:part name="Cred" type="tns:CredInfo"/>
            <wsdl:part name="WebL" type="xsd:string"/>
      </wsdl:message>

        <wsdl:portType name="Service">
            <wsdl:operation name="getCred">
                  <wsdl:input message="tns:GetCredReq"/>
                  <wsdl:output message="tns:GetCredRes"/>
            </wsdl:operation>

<wsdl:binding name="Service" type="tns:Service">
            <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

            <wsdl:operation name="getCred">
                  <soap:operation soapAction=""/>
                  <wsdl:input>
                        <soap:body use="literal"/>
                  </wsdl:input>
                  <wsdl:output>
                        <soap:body use="literal"/>
                  </wsdl:output>
            </wsdl:operation>

0
 
LVL 15

Expert Comment

by:bpmurray
ID: 16896774
Maybe something like:

            private static void printSpaces(int depth) {
                  for (int iX=0; iX<depth; iX++) {
                        System.out.print("   ");
                  }
            }
            
            private static void dumpDOM(Node node, int depth) {
                  NodeList nodes = node.getChildNodes();
                  int count = nodes.getLength();
                  for (int iX=0; iX<count; iX++) {
                        Node child = nodes.item(iX);
                        if (child.getChildNodes().getLength() > 0) {
                              printSpaces(depth);
                              System.out.println("<" + child.getNodeName() + ">");
                              printSpaces(depth+1);
                              dumpDOM(child, depth+1); // process children too
                              printSpaces(depth);
                              System.out.println("</" + child.getNodeName() + ">");
                        } else {
                              printSpaces(depth);
                        }
                              System.out.println("<" + child.getNodeName() + " />");
                        }
            }
0
 

Author Comment

by:T_Shah
ID: 16897061

Thanks for your reply.

Well,
There are about 40+ complextype( OBject)  TAG inside that there is an ItemNamed call name
each object has attribute which is Element. Some object has 1 or more.
Now I want to create an XML file that has

<root>
        <complextype name value>
                <emenet name value>
                <emenet name value>
                <emenet name value>
        </complextype name value>
</root>

and when I open that XML file I can see the content of it.
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 16897664
OK, what about this?

            private static void dumpDOM(Node node, int depth, boolean isComplex) {
                  NodeList nodes = node.getChildNodes();
                  int count = nodes.getLength();
                  for (int iX=0; iX<count; iX++) {
                        Node child = nodes.item(iX);
                        Node nameNode = null;
                        String name = child.getLocalName();
                        if (null != name && name.equals("complexType")) {
                              nameNode = child.getAttributes().getNamedItem("name");
                              if (nameNode == null) { // Could be from the parent <xsd:element>
                                    if (child.getParentNode().getLocalName().equals("element")) {
                                          nameNode = child.getParentNode().getAttributes().getNamedItem("name");
                                    }
                              }
                              if (nameNode != null) { // Only print this if available.
                                    printSpaces(depth);
                                    System.out.println("<" + nameNode.getNodeValue() + ">");
                              }
                              dumpDOM(child, depth+1, true); // process children too
                              if (nameNode != null) {
                                    printSpaces(depth);
                                    System.out.println("</" + nameNode.getNodeValue() + ">");
                              }
                        } else if (isComplex) { // We're inside a complex type
                              if (null != name && name.equals("element")) {
                                    nameNode = child.getAttributes().getNamedItem("name");
                              }
                              if (nameNode != null) { // Only print this if available.
                                    printSpaces(depth);
                                    System.out.println("<" + nameNode.getNodeValue() + ">");
                              }
                              dumpDOM(child, depth+1, true); // process children too
                              if (nameNode != null) {
                                    printSpaces(depth);
                                    System.out.println("</" + nameNode.getNodeValue() + ">");
                              }
                        } else if (child.getChildNodes().getLength() > 0) {
                              dumpDOM(child, depth, false); // process children too
                        }
                  }
            }
0
 

Author Comment

by:T_Shah
ID: 16904507
Thanks for your reply. It is getting the tags... but the thing that I really wants to do is as following...

I have schema file as bellow:

<!ELEMENT WEBSERVICES (URL,INTRO,SCHEMA,METHODS)>
<!ELEMENT SCHEMA (OBJECT+)>
<!ELEMENT OBJECT (NAME,ATTRIBUTES)>
<!ELEMENT ATTRIBUTES (ATTRIBUTE+)>
<!ELEMENT ATTRIBUTE (NAME,TYPE,NILLABLE,REMARKS,ALLOWEDVALUES?)>
<!ELEMENT METHODS (SUBSECTION+) >
<!ELEMENT SUBSECTION (PARA+,METHOD+) >
<!ELEMENT METHOD (RETURN,DESCRIPTION,PARAMETERS*,EXAMPLES?,NOTES?,SEEALSO?) >
<!ELEMENT RETURN (#PCDATA) >
<!ATTLIST SUBSECTION title CDATA  #REQUIRED>
<!ATTLIST METHOD name CDATA #REQUIRED>

I would like to create XML file (Not on console) according to the DTD schema as mention above.
The Object is Complextype which has has a name = "test".
Each Object has attributes 1 or more.
Each attribute has (NAME,TYPE,NILLABLE,REMARKS,ALLOWEDVALUES?)
It is possible that there is no remark or allowedvalues in wsdl file. But I would like to create those tags so I can add some data later on.
Same thing for other as well.
How would I be able to do that?

Is it posible to do with Converting an XML Fragment into a DOM Fragment. If so, How would I do that?

Thanks in advance!!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:bpmurray
ID: 16905010
The code above should load a well-formed XML fragment into a DOM, so you can process it as you want.

I'm afraid I don't understand what you want. Do you want same sample data? Or do you want a skeleton of an XML file that would match the schema?
0
 

Author Comment

by:T_Shah
ID: 16905685
I want to get the data from wsdl file and feed it in to new XML file that match to the schema. so final output of XML file will have all the element that are described in schema file.

Thanks
0
 

Author Comment

by:T_Shah
ID: 16936767
Hello bpmurray:
I really appreciate your help. I have figure it out and it works fine.
Once again Thanks....
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 16937251
Delighted!
0
 

Author Comment

by:T_Shah
ID: 16937637
Hello:
I have created 2 xml files T1.xml and T2.xml. Now I am trying to create new XML file
like this:

<root>
      <test>
            <sub>
                  <sub1/>
            </sub>
      </test>
</root>

and want to append T1.xml right ater </sub>.
And I want to append T2.xml right after T1.xml ends.

How would I able to append in Java?

Thanks in advance!!!
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 16937706
Don't have too much time today :-)

Read T1 into a DOM
Read T2 into a DOM

Print the constant stuff above
When you get to sub, start dumping your T1 DOM
When that's finished, dump the T2 DOM
Continue the constant stuff.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now