Android no validating documentbuilder implementation available
But then again, validating XML against XSD files is a rather heavy weight problem.As for a tool to do this for you, XMLFox seems to be a decent freeware solution, but not having used it personally I can't say for sure.It has built-in support for schema validation, and can convert Java objects to an XML document and vice-versa. In debian squeeze it is located in the package "libxerces-c-samples". As far as libraries goes, pretty much the de-facto standard is Xerces2 which has both C and Java versions.Castor and JAXB are other Java libraries that serve a similar purpose to XMLBeans. The definition of the dtd and xsd has to be in the xml! Be fore warned though, it is a heavy weight solution. Source xml File = new Stream Source(new File("web.xml")); Schema Factory schema Factory = Schema Factory Instance(XMLConstants. URL schema File = new URL(" // webapp example xsd: // URL schema File = new URL(" // local file example: // File schema File = new File("/location/to/localfile.xsd"); // etc.
Here's a blog entry showing how that is done: If you are generating XML files programatically, you may want to look at the XMLBeans library.
Or you can set your own via set Resource Resolver, ex: Source xml File = new Stream Source(xml File Location); Schema Factory schema Factory = Schema Factory Instance(XMLConstants.
W3C_XML_SCHEMA_NS_URI); Schema schema = schema Schema(); Validator validator = Validator(); Resource Resolver(new LSResource Resolver() ); validator.validate(xml File); // parse an XML document into a DOM tree Document Builder parser = Document Builder Instance()Document Builder(); Document document = parser.parse(new File("instance.xml")); // create a Schema Factory capable of understanding WXS schemas Schema Factory factory = Schema Instance(XMLConstants.
But in this example I used a Java String, which is UTF-16 encoded, without a prologue. The answer is that the parser did not read the string directly.
Instead it read from an doesn't match the actual content.