Skip to main content

Java - XML Parsing

1 reply [Last post]
p_bunny
Offline
Joined: 2005-09-05

have an xml sturcture as shown below:

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:

or like this....................

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

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sekhar
Offline
Joined: 2003-06-25

This forum is meant for discussions on JAXB 2.0 and JAX-WS 2.0. The above problem does not relate to either of these two.