Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 7th, 2005, 10:45 PM
Registered User
 
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Java - XML parsing

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


 
Old September 8th, 2005, 01:59 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Please do not post identical questions in two categories, at least not without stating at the top of the post you have done so.
How would you feel if you spent twenty minutes responding to someone's post only to find it had been answered correctly already in a separate category?

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
parsing a flat file in java Rod Guteriez BOOK: Beginning Java 2 2 May 18th, 2009 09:11 AM
XML parsing denzil_cactus Perl 0 June 11th, 2007 02:34 AM
java xml parsing p_bunny XML 1 September 8th, 2005 01:55 AM
XML Parsing tgopal Javascript 2 July 27th, 2004 08:54 AM
Parsing special characters from java to excel dracuella J2EE 0 May 19th, 2004 05:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.