convert XSD to XML using DOM

Discussion in 'Java' started by kiranfpatil, Apr 12, 2011.

  1. kiranfpatil

    kiranfpatil

    Joined:
    Apr 12, 2011
    Messages:
    1
    this code has errors, DOM is not able to parse it corectly

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.xml.sax.SAXException;
    import javax.xml.transform.*;
    import java.util.Iterator;
    import java.util.Vector;
    import com.sun.xml.xsom.XSFacet;
    import com.sun.xml.xsom.XSRestrictionSimpleType;
    import com.sun.xml.xsom.XSSimpleType;
    public class XSDtoXML {

    public static void main (String[] args){

    try {

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();//3
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();//3
    //Document dom = null;//2
    Document document = documentBuilder.newDocument();
    document = documentBuilder.parse("C:\\Users\\kiran_patil\\Desktop\\sample.xsd");
    System.out.println("document parsed");
    System.out.println("appending in order to convert to XML");
    System.out.println(document.getDocumentElement());

    while(document.getParentNode()!=null)
    {
    System.out.println("*");
    Element rootElement = document.createElement(document.getNodeName());
    document.appendChild(rootElement);
    if(document.getChildNodes()!=null)
    {
    Element em = document.createElement(document.getNodeName());
    em.appendChild(document.createTextNode(document.getNodeValue()));
    rootElement.appendChild(em);
    while(document.getNextSibling()!=null)
    {
    Element em1 = document.createElement(document.getNodeName());
    em1.appendChild(document.createTextNode(document.getNodeValue()));
    rootElement.appendChild(em1);
    }
    }
    }
    System.out.println("transformation started");
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(System.out);
    transformer.transform(source,result);
    System.out.println("transformation end");
    }
    catch(ParserConfigurationException pce)
    {
    pce.printStackTrace();
    }
    catch(SAXException se)
    {
    se.printStackTrace();
    }
    catch(IOException ioe)
    {
    ioe.printStackTrace();
    }
    catch(TransformerConfigurationException tce)
    {
    tce.printStackTrace();
    }
    catch(TransformerException te)
    {
    te.printStackTrace();
    }

    }
    }
    kiranfpatil, Apr 12, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,081
    Markus
    Nov 22, 2005
  2. Matt
    Replies:
    3
    Views:
    37,204
    MKNDK
    Sep 11, 2008
  3. stiank81
    Replies:
    4
    Views:
    15,351
    stiank81
    Jun 26, 2005
  4. manjunath.d@gmail.com

    Convert a XML DOM Object to a HTML DOM Object

    manjunath.d@gmail.com, Sep 20, 2005, in forum: XML
    Replies:
    0
    Views:
    537
    manjunath.d@gmail.com
    Sep 20, 2005
  5. Rushi
    Replies:
    1
    Views:
    541
    the_edge123.nospam@club-internet.fr
    Dec 9, 2005
Loading...

Share This Page