Solved

How to create XML Document from WSDL File?

Posted on 2006-06-12
12
508 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Delighted!
0
 

Author Comment

by:T_Shah
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Adobe Customization Wizard XI issues 26 164
Microsoft Edge 9 79
bunnyEars challenge 6 62
changeXy challenge 13 56
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.
This is about my first experience with programming Arduino.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

15 Experts available now in Live!

Get 1:1 Help Now