The goal is to insert some element into an existing xml document according to a given XSD as part of a java appliaction.
The problem is: the concrete XSD is only known at runtime, so the standard solutions like jaxb don't work since they handle XSDs known at compile-time.
The only structural information I have at compile-time is the structure of the element to insert and the name of the parent element. The order and names of the siblings of that element is only available inside the XSD at runtime.
From a higher level I would say the task can be done in the following steps:
The problem is step 2. I found a similar question asking for a solution of that step: How to load XML tags order from XSD with Java? but this question has no answer.
You could try using the XML Schema Object Model to figure out the order among sibling elements. I have used it in a project to determine the cardinality of child elements, which was important when transforming XML to JSON and vice versa.
It takes some effort to grok it, but it works quite well.
UPDATE
I think XSOM is part of Xerces, or at least Xerces implements Post Schema Validation Infoset (PSVI). Given that you parse the input XML document using a schema aware parser, you can access the PSVI and analyze it. Read more here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With