Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Issue in validating vanila XML

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need help in validating with the existing XML and existing XSD:
---------------------------------------------------------------

These are the entities used:
----------------------------
XSD FILE (mainInfo.xsd):
========================


XML File:
=========


ERROR MESSAGE:
==============
Error : cvc-elt.1: Cannot find the declaration of element 'mainTag'
IF I CHECK WITH FOLLOWING STARTING TAG IT WORKS PERFECTLY But following way we can't change from the thirdparty's XML:
=======================================
<mainTag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns="http://somesite.com/schemas/BaseDataTypes";
xsi:schemaLocation="http://somesite.com/schemas/BaseDataTypes http://somesite.com/schemas/mainInfo.xsd">

I USED THE FOLLOWING CODE FOR VALIDATING AGAINST THE XSD:
=========================================================

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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;


public class DomValidator {
static final String schemaSource = "c:\\testProject\\mainInfo.xsd";

static final String xml_file = "c:\\testProject\\profile.xml";

static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";

static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";

public static void main(String args[]) {


try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);


factory.setAttribute(JAXP_SCHEMA_LANGUAGE,
"http://www.w3.org/2001/XMLSchema";);
factory.setAttribute(JAXP_SCHEMA_SOURCE, new File(schemaSource));


DocumentBuilder domParser = factory.newDocumentBuilder();
// Set Error Handler
ErrorChecker errors = new ErrorChecker();
domParser.setErrorHandler(errors);

Document document = domParser.parse(new File(xml_file));
List errorList = errors.getErrors();
Iterator iter = errorList.iterator();
while (iter.hasNext()) {
String errroMessage = (String) iter.next();
System.out.println(errroMessage);
}
} catch (Exception e) {
e.printStackTrace();
}

}
}

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

class ErrorChecker extends DefaultHandler

{
public ArrayList errorList = new ArrayList();

public ErrorChecker() {
}

public List getErrors() {
return errorList;
}

public void error(SAXParseException e) {
errorList.add("Error : " + e.getMessage() + " " + e.getPublicId() + " "
+ e.getSystemId());
}

public void warning(SAXParseException e) {
errorList.add("Warning : " + e.getMessage());
}

public void fatalError(SAXParseException e) {
errorList.add("Fatal error : " + e.getMessage());
System.exit(1);
}

}



It would be great if anyone can suggest me without changing the existing XML structure to validate properly by changing in My Java program. Or guide me to the right direction would be appreiciated.
 
Marshal
Posts: 25671
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know much about Schema. But what you are saying is, if you declare your mainTag element to be in the default namespace then the XML validates, and if you declare it to be in no namespace, then it doesn't validate.

And I observe that your schema has a targetNamespace attribute whose value just happens to be the same as the default namespace URI which makes the XML validate.

So if this were my problem, I would go to my Schema reference material and find out what the targetNamespace attribute was supposed to do and what would happen if I left it out.
 
joshua asem
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HMM...

targetNamespace="http://somesite.com/BaseDataTypes";
AND
schemaLocation="http://somesite.com/BaseDataTypes.xsd";
should be same in the XSD file, So can't change with that.

Also these xsd and xml are third-party's files and I have only the Java program which need to tailored to fit those guys. And the given xsd and xml properly validates with the XMLSpy. I am wondering is there anything which can change the attributes in the above java program to fit those guys and validate properly. Still wondering
[ August 21, 2008: Message edited by: joshuaa ]
 
Marshal
Posts: 67418
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"joshuaa", please check your private messages for an important administrative matter.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic