Skip to content

Instantly share code, notes, and snippets.

@mermshaus
Created August 31, 2012 09:17
Show Gist options
  • Save mermshaus/3550658 to your computer and use it in GitHub Desktop.
Save mermshaus/3550658 to your computer and use it in GitHub Desktop.
Snippet for running Apache Xerces (Java version) to validate XML files with catalog and schema support
<?xml version="1.0"?>
<!DOCTYPE catalog PUBLIC "-//OASIS//DTD XML Catalogs V1.0//EN"
"file:///usr/share/xml/schema/xml-core/catalog.dtd">
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog" prefer="public">
<uri name="http://www.sitemaps.org/schemas/sitemap/0.9"
uri="sitemap.xsd"/>
<uri name="http://www.google.com/schemas/sitemap-news/0.9"
uri="sitemap-news.xsd"/>
</catalog>
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
<url>
<loc>URL-ZUM-ARTIKEL</loc>
<news:news>
<news:publication>
<news:name>BEZEICHNUNG MEINER SEITE</news:name>
<news:language>de</news:language>
</news:publication>
<news:genres>PressRelease</news:genres>
<news:publication_date>2012-08-28T08:00:00+02:00</news:publication_date>
<news:title>TITEL DES ARTIKELS</news:title>
<news:keywords>tag1, tag2, tag3, tag4, tag5</news:keywords>
</news:news>
</url>
</urlset>
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import org.xml.sax.SAXException;
import com.sun.org.apache.xerces.internal.util.XMLCatalogResolver;
public class SchemaValidator
{
public void run() throws Exception
{
Source xmlFile = new StreamSource(new File("demo.xml"));
SchemaFactory schemaFactory = SchemaFactory.newInstance(
XMLConstants.W3C_XML_SCHEMA_NS_URI);
String[] catalogs = { "catalog.xml" };
XMLCatalogResolver resolver
= new XMLCatalogResolver(catalogs, true);
schemaFactory.setResourceResolver(resolver);
Schema schema = schemaFactory.newSchema();
Validator validator = schema.newValidator();
validator.setResourceResolver(
schemaFactory.getResourceResolver());
try {
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId()
+ " is valid");
} catch (SAXException e) {
System.out.println(xmlFile.getSystemId()
+ " is NOT valid");
System.out.println("Reason: "
+ e.getLocalizedMessage());
}
}
public static void main(String[] args)
{
SchemaValidator app = new SchemaValidator();
try {
app.run();
} catch (Exception e) {
System.out.println(e);
}
}
}
@asciarra
Copy link

asciarra commented Aug 6, 2015

Hi,
I was trying out your snippet but it fails with this error: "org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 65; cvc-elt.1: Cannot find the declaration of element 'urlset'."

Don't know if I have to do something else than downloading the two xsd from google and put them on my classpath.

If you got this working I would really like to know how.

Thanks,
Angelo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment