keepworking
asked on
please help on a bug --XML URGENT
----------this is the xsd file-----------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualif ied">
<xs:element name="IMPORTER">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="CONTENT_SRC"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CONTENT_SRC">
<xs:complexType>
<xs:sequence>
<xs:element name="ALIAS" type="xs:string" minOccurs="1"/>
<xs:element name="TEXT" type="xs:string" minOccurs="1"/>
<xs:element name="NOTES" type="xs:string" minOccurs="0"/>
<xs:element name="DATA_SOURCE" type="xs:string" minOccurs="0"/>
<xs:element name="IS_CLIENT" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
-------------------------- ---------- ---------- ---------- ---------- ---------
-------------------this is the xml passed systax validation by method below
<?xml version="1.0" encoding="UTF-8"?>
<IMPORTER xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocat ion="impor ter.xsd">
<CONTENT_SRC>
<ALIAS>124324</ALIAS>
<TEXT>Hello</TEXT>
<NOTES></NOTES>
<DATA_SOURCE></DATA_SOURCE >
<IS_CLIENT></IS_CLIENT>
</CONTENT_SRC>
</IMPORTER>
-------------------------- ---------- ---------- ---------- ---------- ---------- ----------
public static boolean validateXMLSyntax(String fileName, String schemaFile )
throws IOException, SAXException
{
SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstan ts.W3C_XML _SCHEMA_NS _URI);
Source schemaSource = new StreamSource(new File(schemaFile));
Schema schema = schemaFactory.newSchema(sc hemaSource );
Validator validator = null;
ImporterErrorHandler mySchemaErrorHandler = new ImporterErrorHandler();
try
{
validator = schema.newValidator();
validator.setErrorHandler( mySchemaEr rorHandler );
validator.validate(new StreamSource(fileName));
} catch (SAXException e)
{
System.out.println("Except ion thrown in ValidateXML.validateXMLSyn tax() - my exception: " + e);
}
setReport( mySchemaErrorHandler.getEr ror() );
if ( getReport() == null )
{
return true;
}
return false;
}
-------------------------- ---------- ---------- ----questi on is continued----------------- ---------- --
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualif
<xs:element name="IMPORTER">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="CONTENT_SRC"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CONTENT_SRC">
<xs:complexType>
<xs:sequence>
<xs:element name="ALIAS" type="xs:string" minOccurs="1"/>
<xs:element name="TEXT" type="xs:string" minOccurs="1"/>
<xs:element name="NOTES" type="xs:string" minOccurs="0"/>
<xs:element name="DATA_SOURCE" type="xs:string" minOccurs="0"/>
<xs:element name="IS_CLIENT" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
--------------------------
-------------------this is the xml passed systax validation by method below
<?xml version="1.0" encoding="UTF-8"?>
<IMPORTER xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocat
<CONTENT_SRC>
<ALIAS>124324</ALIAS>
<TEXT>Hello</TEXT>
<NOTES></NOTES>
<DATA_SOURCE></DATA_SOURCE
<IS_CLIENT></IS_CLIENT>
</CONTENT_SRC>
</IMPORTER>
--------------------------
public static boolean validateXMLSyntax(String fileName, String schemaFile )
throws IOException, SAXException
{
SchemaFactory schemaFactory = SchemaFactory.newInstance(
Source schemaSource = new StreamSource(new File(schemaFile));
Schema schema = schemaFactory.newSchema(sc
Validator validator = null;
ImporterErrorHandler mySchemaErrorHandler = new ImporterErrorHandler();
try
{
validator = schema.newValidator();
validator.setErrorHandler(
validator.validate(new StreamSource(fileName));
} catch (SAXException e)
{
System.out.println("Except
}
setReport( mySchemaErrorHandler.getEr
if ( getReport() == null )
{
return true;
}
return false;
}
--------------------------
try this method,
public static boolean validateXMLSyntax(String fileName, String schemaFile )
throws IOException, SAXException, JAXBException, ParserConfigurationExcepti on
{
try
{
final String sl = XMLConstants.W3C_XML_SCHEM A_NS_URI;
SchemaFactory factory = SchemaFactory.newInstance( sl);
StreamSource ss = new StreamSource(schemaFile );
Schema schema = factory.newSchema(ss);
SAXParserFactory spf = SAXParserFactory.newInstan ce();
spf.setSchema(schema);
SAXParser parser = spf.newSAXParser();
parser.parse(new File ( fileName ),new DefaultHandler () );
return true ;
}
catch ( Exception e )
{
return false ;
}
}
public static boolean validateXMLSyntax(String fileName, String schemaFile )
throws IOException, SAXException, JAXBException, ParserConfigurationExcepti
{
try
{
final String sl = XMLConstants.W3C_XML_SCHEM
SchemaFactory factory = SchemaFactory.newInstance(
StreamSource ss = new StreamSource(schemaFile );
Schema schema = factory.newSchema(ss);
SAXParserFactory spf = SAXParserFactory.newInstan
spf.setSchema(schema);
SAXParser parser = spf.newSAXParser();
parser.parse(new File ( fileName ),new DefaultHandler () );
return true ;
}
catch ( Exception e )
{
return false ;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Check if you have an empty line on the top of the file (before the xml declaration).
So what was the problem? And why the C grade, you can ask for more clarification if you want?
ASKER
"http://www.23.org/2001/XMLSchema-instance
the real problem http://www.23.org/, it should be changed to http://www.w3.org
I gave C, because none find the problem and give me any usful hint.
the real problem http://www.23.org/, it should be changed to http://www.w3.org
I gave C, because none find the problem and give me any usful hint.
You're supposed to at least get back to the experts and say that, before closing it, if you need more assistance.
ASKER
do you think you don't deserve that 500 points?
That's not the point~! The point is that if you still face problems, tell us so that we can think of ways to help you better. If you stay quiet, we are not mind readers to assume what is happening at your end,
ASKER
----------------------I think the 2 xml files are same except the records size. so my method in java is wrong?????????????????????
-------
--------the error message for this file is Parse Fatal Error Line: 2 Message: The processing instruction target matching "[xX][mM][lL]" is not allowed.??????????????????
<?xml version="1.0" encoding="UTF-8"?>
<IMPORTER xmlns:xsi="http://www.23.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocat
<ALIAS>211 </ALIAS>
<TEXT>Load Game </TEXT>
<NOTES>UI </NOTES>
<DATA_SOURCE>gam_mars_db.m
<IS_CLIENT>true </IS_CLIENT> </CONTENT_SRC> <CONTENT_SRC> <ALIAS>210 </ALIAS> <TEXT>Save Game </TEXT> <NOTES>UI </NOTES> <DATA_SOURCE>gam_mars_db.m