0

So I have an XML that already has all the schemas specified in the root tag like this

<Waybill xmlns="urn:some:uri1"
     xmlns:cac="urn:some:uri2"
     xmlns:cbc="urn:some:uri3"
     xmlns:ext="urn:some:uri4" xmlns:xxx="http://some.url.com">

Is it possible to validate my XML against these in the Java code in such a way that I don't have to repeat them in the Java code again? All the examples I found so far require you to specify the url explicitly in the code

Kamil Janowski
  • 1,872
  • 2
  • 21
  • 43
  • My XML is a little rusty, but those are just namespaces. The URIs don't really mean much more than a unique string, and they're definitely not schemas. [Here's an XML schema](https://en.wikipedia.org/wiki/Document_type_definition#XML_DTD_schema_example). – David Ehrmann Nov 10 '15 at 06:00
  • cac or cbc in this case is a namespace but URIs point to schemas, don't they? At least when I open the file that the URI points to, it contains another XML file that starts with – Kamil Janowski Nov 10 '15 at 06:12
  • plus Intellij Idea actually validates the XML against those schemas listed in the top and underlines whatever funny change I make in my XML – Kamil Janowski Nov 10 '15 at 06:15
  • Intellij is 80% magic. – David Ehrmann Nov 10 '15 at 06:22
  • Try this: https://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setValidating(boolean) Might need to use the setNamespaceAware property as well. DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance(); dbf.setValidating(true) DocumentBuilder builder=factory.newDocumentBuilder(); Document doc=builder.parse(new File(filename)); – trappski Nov 10 '15 at 07:48

1 Answers1

0

Try this: Validating XML against XSD

The magic seems to be in this: XMLConstants.W3C_XML_SCHEMA_NS_URI.

Community
  • 1
  • 1
David Ehrmann
  • 7,366
  • 2
  • 31
  • 40