Reputation: 87
Im parsing XML documents with java. Every document has root tag (it is a string) and a number of tags with text(unknown number) for example(check code in codebox). <AnyStrYouwant>
tags have a string of characters in its body.
<anyRoot>
<AnyStrYouwant1>anyTextYouWant1</AnyStrYouwant1>
<AnyStrYouwant2>anyTextYouWant2</AnyStrYouwant2>
...
</anyRoot>
How programically(in java) chek if some file suits this structure. I can parse XML, I know that there is DTD(for example) that can check XML file with known format (tag names and content). What shall I use in this case?
PS: some people advice me to use XSD. But if I want to validate elements I need to know root element name. I dont know root element name (every file has own root element).
Upvotes: 0
Views: 1378
Reputation: 955
You can check XML with XSD using this sample code.
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.InputSource;
public boolean isValidXML(InputStream is) {
InputSource isrc;
try {
isrc = new InputSource(new FileInputStream("path/your-xsd-file.xsd")));
SAXSource sourceXSD = new SAXSource(isrc);
SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema")
.newSchema(sourceXSD).newValidator()
.validate(new StreamSource(is));
} catch (Exception e) {
return false;
}
return true;
}
Upvotes: 0
Reputation: 582
You can use DTD or XSD to validate XML, take a look at :
http://www.w3schools.com/xml/xml_dtd.asp
http://www.journaldev.com/895/how-to-validate-xml-against-xsd-in-java
XSD is the advanced technique to validate XML, it's more flexible than DTD but you can use one of those technologies to solve your problem.
Upvotes: 0
Reputation: 83
I cant comment with my new account but yes you can use DTD, Schematron Schematron is much more flexible and it is industry standart where DTD is really a legacy technology but still widely used. DTD will check for allowed tags (in short) where Schematron is able to check the structure of the file for example that some special tags should be in first 10 lines of XML etc.
I would use DTD if you are only checking for existing tags and attributes allowed values. If you do something more complex I would recommend using Schematron with its rules based validation.
Upvotes: 1