Skip to main content

append to xml

4 replies [Last post]
refactoring
Offline
Joined: 2005-08-29
Points: 0

Dear all,
I need to add various of object's attributes into XML constantly----I need to use JAxb to append xml content then write it to xml file , and then repeat this step again and again.Can I use Jaxb to do so ? If so, how can I do that. Thanks.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kohsuke
Offline
Joined: 2003-06-09
Points: 0

I'm not sure if I really understand what you are describing. Maybe you are saying that you have an XML file, you add a sub-tree to this XML (which is represented by JAXB-bound objects), and you write it back to a disk (and you need to do this in a relatively efficient way)?

Or are you saying that you have an XML document, and you need to put additional attributes here and there (like RDF or xlink?) and write it back?

If you could elaborate more on the problem, I think it's easier for us to help.

refactoring
Offline
Joined: 2005-08-29
Points: 0

Dear Kohsuke,

Sorry , I make you confused. Similary as the first way which you understand. I have one xml file. And , I have various objects. Also, those objects are not the same, they have difference attributes. What I need to do is append those object to my xml file then write to disk. So, my question is whether JAXB can do this job or not. Because Since I cannot find any examples to Jaxb to append sub-tree to xml .

Thanks for your help.

kohsuke
Offline
Joined: 2003-06-09
Points: 0

The easiest way to do such things is to read XML into a DOM tree (by using JAXP), then use JAXB to update this DOM tree, and write that DOM back to a file (by using JAXP.)

For example, you can use DOM to remove some elements, and you can use JAXB to marshal into a new child of an existing element by using the marshal method that takes Node.

Writing a DOM back to a file is the ugly part, where you need to use an identity transformer.

A variation of this would be to use other DOMs, like dom4j, JDOM, and XOM. Most of them come with a Result implementation which allows JAXB to write directly to its sub-tree, much like DOM. It's generally much easier to read/write a file to DOM by using those technologies than W3C DOM.

To do this more efficiently, you need to use a technology like StAX and stitch it nicely to JAXB. This is somewhat dependent on the kind of changes you'll be making, so it's difficult to talk in general.

refactoring
Offline
Joined: 2005-08-29
Points: 0

Thank so much!