0

i have some already generated xmls and the application causing problems now needs to add elements to it which need to be at a specific position to be valid with to the applied schemata...

now there are two problems the first one is that i have to hardcode the positions which is not that nice but "ok". But the much bigger one is jdom... I printed the content list and it looks like:

element1
text
element2
element4
text
element5

while the textnodes are just whitespaces and every element i add makes it even more unpredictable how many textnodes there are (because sometimes there are added some sometimes not) which are just counted as it were elements but i want to ignore them because when i add element3 at index 2 its not between element2 and element4 it comes after this annoying textnode.

Any suggestions? The best solution imho would be something that automatically puts it where it has to be according to the schema but i think thats not possible?

Thanks for advice :)

Dennis Ich
  • 3,585
  • 6
  • 27
  • 44

1 Answers1

1

The JDOM Model of the XML is very literal... it has to be. On the other hand, JDOM offers ways to filter and process the XML in a way that should make your task easier.

In your case, you want to add Element content to the document, and all the text content is whitespace..... so, just ignore all the text content, and worry about the Element content only.

For example, if you want to insert a new element nemt before the 3rd Element, you can:

rootemt.getChildren().add(3, new Element("nemt"));

The elements are now sorted out.... what about the text...

A really simple solution is to just pretty-print the output:

XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
xout.output(System.out, mydoc);

That way all the whitespace will be reformatted to make the XML 'pretty'.

EDIT - and no, there is no way with JDOM to automatically insert the element in the right place according to the schema....

Rolf

rolfl
  • 17,539
  • 7
  • 42
  • 76