Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > convert XSD to XML using DOM

Reply
Thread Tools

convert XSD to XML using DOM

 
 
kiranfpatil kiranfpatil is offline
Junior Member
Join Date: Apr 2011
Posts: 1
 
      04-12-2011
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\\Des ktop\\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.ge tNodeValue()));
rootElement.appendChild(em);
while(document.getNextSibling()!=null)
{
Element em1 = document.createElement(document.getNodeName());
em1.appendChild(document.createTextNode(document.g etNodeValue()));
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();
}

}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert XML to XSD? or DTD to XSD? Matt XML 3 09-11-2008 12:40 PM
Validation of XSD (XML Schema) against XSD Rushi XML 1 12-09-2005 08:12 AM
XML + XSD: Is it possible to get all errors against the XSD? Markus Java 1 11-22-2005 02:53 PM
Convert a XML DOM Object to a HTML DOM Object manjunath.d@gmail.com XML 0 09-20-2005 08:16 AM
XML schema - Make xsd include another xsd stiank81 XML 4 06-26-2005 05:11 PM



Advertisments