2

Suppose I have a very simple input exmaple XML file as follows

<?xml version="1.0"?>
<content>
    <some />
</content>

I would like to modify the xml structure by inserting additional elements anywhere in the original structure, or replace an element with other content.

Can I somehow achieve that using EclipseLink MOXy? E.g. I want to to replace "some" by "someReplacement" and add "whatever".

<?xml version="1.0"?>
<content>
    <someReplacement>
        <more>information</more>
    </someRepaclement>
    <whatever />
</content>

The actual XML I want to process is more complex, however I only actually deal with a small subset of its content, so I would prefer not to unmarshall the complete file into a complex bean structure, make changes to a small set of elements, and marshall the whole structure back into a file. At least I don't want to know about the complexity.

This is because the input XML schema can vary greatly, but the specific elements I care about exist in each of these schema. So I would ideally want to find a solution to e.g. adapt XPaths in something like a bindings file to point to the elements I want to replace/insert.

I would prefer not to use JDOM, because the elements I produce for insertion/replacement are complex and I don't want to create them 'by hand' but instead have some bean structure be mapped.

Can I do this with MOXy? Any other JAXB provider? Should I use JDOM, or is there anything else that could help?

hansi
  • 2,278
  • 6
  • 34
  • 42
  • I think this may actually help: http://stackoverflow.com/questions/4230499/java-to-xml-conversions (Use Case #5), http://blog.bdoughan.com/2010/09/jaxb-xml-infoset-preservation.html. I will try and report back – hansi Jul 08 '13 at 20:23
  • If efficiency is important, you might want to look into vtd-xml – vtd-xml-author Jul 20 '13 at 23:35

3 Answers3

1

This solution worked for me:

Java to XML conversions? (Use Case #5)

http://blog.bdoughan.com/2010/09/jaxb-xml-infoset-preservation.html.

Community
  • 1
  • 1
hansi
  • 2,278
  • 6
  • 34
  • 42
0

You should use XSLT. The reason XSLT was invented, was to modify XML structures.

forty-two
  • 12,204
  • 2
  • 26
  • 36
  • I think XSLT is too limited for my use case. From the XML structures I take as input I generate new content, e.g. using external data sources, which I then use to add or replace in the input content. From my understanding I cannot get this done with XSLT. – hansi Jul 08 '13 at 05:07
0

Below is the code of perform this task in vtd-xml. VTD-XML is the only xml processing framework supporting incremental update...

Here is an article explaining this feature...

http://www.devx.com/xml/Article/36379

import java.io.*;
import com.ximpleware.*;

public class simpleUpdate {
    public static void main(String[] args) throws VTDException, IOException{
        // TODO Auto-generated method stub
        VTDGen vg = new VTDGen();
        if (!vg.parseFile("input.xml", false))
            return;
        VTDNav vn = vg.getNav();
        XMLModifier xm = new XMLModifier(vn);
        if (vn.toElement(VTDNav.FC)){
            xm.remove();
            xm.insertAfterElement(" <someReplacement>\n<more>information</more>\n</someRepaclement><whatever/>");
            xm.output("output.xml");
        }
    }
}
vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30