java xml parsing [message #136414] |
Wed, 07 September 2005 23:17 |
p_bunny
Messages: 1 Registered: September 2005 Location: india
|
Junior Member |
|
|
JAVA XML PArsing
--------------------------------------------------------------------------------
have an xml sturcture as shown below:
<cnv:conversion xmlns:cnv="urn:ssis-conv" xmlns:xsql="urnracle-xsql">
<cnv:roottag support="yes" destag="xfw:script">
<cnv:addattr name="debug" value="no" />
<cnv:addattr name="xmlns:xfw" value="urn:scb-xfw-v2" />
</cnv:roottag>
<xsql:query support="yes" destag="xfw:genxml">
<connection support="no" />
<row-element support="yes" destag="row-name" />
<rowset-element support="yes" destag="rowset-name" />
<id-attribute support="no" />
<tag-case support="no" />
<null-indicator support="no" />
<cnv:addattr name="debug" value="yes" />
<cnv:addattr name="Attribute1" value="Value1" />
</xsql:query>
<xsql:no-rows-query support="no" child="no" />
<responsedata support="no" child="yes" />
<responsedata2 support="no" child="yes" />
<page destag="page1" support="yes">
<connection support="no" /> <debug destag="MyDebug" />
<cnv:addattr name="tt" value="t1" />
</page>
<xsql:set-page-param support="yes" destag="setpageparam">
<name destag="myname" />
</xsql:set-page-param>
<xsql:set-page-param-local support="yes" destag="Myset-page-param-local">
<name destag="mylocalname" />
</xsql:set-page-param-local>
</cnv:conversion>
I want this xml to be validated by the dom parser which i had done and its showing it as valid XML.
If there are any repeating tags like:
<cnv:conversion xmlns:cnv="urn:ssis-conv" xmlns:xsql="urnracle-xsql">
<cnv:conversion xmlns:cnv="urn:ssis-conv" xmlns:xsql="urnracle-xsql">
<cnv:roottag support="yes" destag="xfw:script">
<cnv:addattr name="debug" value="no" />
<cnv:addattr name="xmlns:xfw" value="urn:scb-xfw-v2" />
</cnv:roottag>
<xsql:query support="yes" destag="xfw:genxml">
<connection support="no" />
<row-element support="yes" destag="row-name" />
<rowset-element support="yes" destag="rowset-name" />
<id-attribute support="no" />
<tag-case support="no" />
<null-indicator support="no" />
<cnv:addattr name="debug" value="yes" />
<cnv:addattr name="Attribute1" value="Value1" />
</xsql:query>
<xsql:no-rows-query support="no" child="no" />
<responsedata support="no" child="yes" />
<responsedata2 support="no" child="yes" />
<page destag="page1" support="yes">
<connection support="no" /> <debug destag="MyDebug" />
<cnv:addattr name="tt" value="t1" />
</page>
<xsql:set-page-param support="yes" destag="setpageparam">
<name destag="myname" />
</xsql:set-page-param>
<xsql:set-page-param-local support="yes" destag="Myset-page-param-local">
<name destag="mylocalname" />
</xsql:set-page-param-local>
</cnv:conversion>
</cnv:conversion>
or like this....................
<cnv:conversion xmlns:cnv="urn:ssis-conv" xmlns:xsql="urnracle-xsql">
<cnv:roottag support="yes" destag="xfw:script">
<cnv:addattr name="debug" value="no" />
<cnv:addattr name="xmlns:xfw" value="urn:scb-xfw-v2" />
</cnv:roottag>
<cnv:roottag support="yes" destag="xfw:script">
<cnv:addattr name="debug" value="no" />
<cnv:addattr name="xmlns:xfw" value="urn:scb-xfw-v2" />
</cnv:roottag>
<xsql:query support="yes" destag="xfw:genxml">
<connection support="no" />
<row-element support="yes" destag="row-name" />
<rowset-element support="yes" destag="rowset-name" />
<id-attribute support="no" />
<tag-case support="no" />
<null-indicator support="no" />
<cnv:addattr name="debug" value="yes" />
<cnv:addattr name="Attribute1" value="Value1" />
</xsql:query>
<xsql:no-rows-query support="no" child="no" />
<responsedata support="no" child="yes" />
<responsedata2 support="no" child="yes" />
<page destag="page1" support="yes">
<connection support="no" /> <debug destag="MyDebug" />
<cnv:addattr name="tt" value="t1" />
</page>
<xsql:set-page-param support="yes" destag="setpageparam">
<name destag="myname" />
</xsql:set-page-param>
<xsql:set-page-param-local support="yes" destag="Myset-page-param-local">
<name destag="mylocalname" />
</xsql:set-page-param-local>
</cnv:conversion>
or any row is being repeated it not a valid XML....
my java code is :
My java code is shown below :
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
public class sample2
{
public void processXML(String path)
{
File docFile=new File(path);
Document doc=null;
try
{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); DocumentBuilder db=dbf.newDocumentBuilder();
doc=db.parse(docFile);
}
catch(IOException ioe)
{
System.out.println("cant find file");
}
catch(Exception e)
{
System.out.println("Error has occured");
}
Element root=doc.getDocumentElement();
NodeList children=root.getChildNodes();
StringBuffer sbuf=new StringBuffer();
for(Node child=root.getFirstChild(); child!=null; child=child.getNextSibling())
{
if(child.getNodeType()==child.TEXT_NODE) sbuf.append(child.getNodeValue()) ;
else if(child.getNodeType()==child.ELEMENT_NODE) if(child.getChildNodes()!=null)
getnodes(child, sbuf);
}
}
public void getnodes(Node Child, StringBuffer sbf)
{
NodeList children=Child.getChildNodes();
if(children.getLength() > 0)
{
System.out.println(children.getLength() + Child.getNodeName());
}
for(Node child=Child.getFirstChild(); child!=null;child=child.getNextSibling())
{
if(child.getNodeType()==child.TEXT_NODE) sbf.append((child.getNodeValue()).trim());
else if(child.getNodeType()==child.ELEMENT_NODE)
if(child.getChildNodes()!=null)
{
getnodes(child, sbf); sbf.append(";");
}
}
}
public static void main(String[] args)
{
testxml txml=new testxml(); txml.processXML("E:\\Naresh\\conversion_fxsql.xml");
}
}
can any body tell me how to validate the repeating tags with the given code.... if any one of the tags are repeating it should raise an exception thats its not a valid xml
Thanks in advance
Regards
Bunny
|
|
|