Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update xml according to XSD only known at runtime?

Tags:

java

xml

xsd

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:

  1. parse xml to build document tree
  2. determine the right place where to insert the element (<-- this is the hard part) according to the given XSD
  3. insert the element
  4. write the document as xml

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.

like image 814
mschenk74 Avatar asked Dec 20 '25 13:12

mschenk74


1 Answers

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.

like image 114
forty-two Avatar answered Dec 23 '25 14:12

forty-two



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!