• Post Reply Bookmark Topic Watch Topic
  • New Topic

xml validation using DOM parser .  RSS feed

 
lija mandiyan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All ,



I am doing validation of an xml file against an XSD in Java using w3c and DOM parser .At this point I am facing an error . Basically the xsd file which is supposed to be read from the xml is not getting detected at



Please see my code snippet :

import java.io.File;



import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;



import org.w3c.dom.Document;

import org.xml.sax.SAXParseException;

import org.xml.sax.helpers.DefaultHandler;



// to capture the errors and warnings and the fatal errors occured

class ErrorChecker extends DefaultHandler

{

public ErrorChecker()

{

}

public void error (SAXParseException e)

{

System.out.println("Error arsing error: "+e.getMessage()+" "+e.getPublicId() +" "+e.getSystemId());

}

public void warning (SAXParseException e)

{

System.out.println("Warning arsing problem: "+e.getMessage());

}

public void fatalError (SAXParseException e)

{

System.out.println("Fatal error arsing error: "+e.getMessage());

System.out.println("Cannot continue.");

System.exit(1);

}

}



public class DOMValidator

{

public static void main (String args[])

{

File docFile = new File("C:/Java/XFrtSum986864-orig-1.xml");

try

{

//Parsing done by the DocumentBuilder which is actually an

//interfce hence document builder fctory has to be used with

//methods likesetvalidating etc

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

dbf.setNamespaceAware(true);

dbf.setValidating(true);

DocumentBuilder db = dbf.newDocumentBuilder();

ErrorChecker errors = new ErrorChecker();

db.setErrorHandler(errors);

Document doc = db.parse(docFile);





} catch (Exception e)

{

System.out.print("Parsing problem." + e);

}

}

}





Basically the xsd file which is supposed to be read from the xml is not getting detected at all



The xml file has the following at the top to locate the xsd :

<?xml version="1.0" encoding="UTF-8"?>

<RailCarrierFreightStatement

xmlns="http://www.tranxml.org/TranXML/Version4.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="C:/java/RailCarrierFreightStatement_proposed.xsd">





and the error is as follows



Error arsing error: Attribute "SegmentId" is not declared for element "Origin".

Error arsing error: Attribute "SegmentId" is not declared for element "GeographicLocation

Error arsing error: Attribute "SegmentId" is not declared for element "NameStructure

Error arsing error: Attribute "EntityIdentifierCode" is not declared for element "Name

Error arsing error: Attribute "SegmentId" is not declared for element "NameStructure

Error arsing error: Attribute "EntityIdentifierCode" is not declared element "Name

�������������������������





what could be the reason for this .If not can you provide with an alternative in DOM parsing using jdk1.4 .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!