aspose file tools *
The moose likes XML and Related Technologies and the fly likes Best API to update an XML document! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Best API to update an XML document!" Watch "Best API to update an XML document!" New topic
Author

Best API to update an XML document!

Jade Jadaaih
Greenhorn

Joined: Jun 17, 2008
Posts: 21
Hi folks,

I need to update few parts of an XML document, like just the value replacement. The value may be a tag value or an attribute.

In this case, which API would you suggest is good to do it? SAX or DOM or XPath or some thing else you know?

It would be great if you could point me or let me know some code snippets for updating with the suggested API. I have done, parsing an XML document with SAX/DOM. I have also done writing XML documents with SAX, DOM and StAX. I am not sure how to update and which is the best way to do it.

Thanks for your time.

Regards,
Jade


Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42952
    
  73
What is "best" is in the eye of the beholder. You could use DOM, although its API is a bit convoluted, which has lead to the rise of libraries such as JDOM, XOM and dom4j.

I like XOM (Tutorial, Sample Code), but it's really a matter of preference.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12839
    
    5
Questions which occur to me:

1. How big is the XML document compared to your memory available?
2. Are you sure that only replacement of existing values - attributes or text nodes - is required? No structure changes?
3. Do changes require knowledge of contents of the rest of the document or are they independent?

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18997
    
    8

I would consider using XSLT for this. For minor changes to a document, start with the identity transformation and add templates which make those minor changes.
Jade Jadaaih
Greenhorn

Joined: Jun 17, 2008
Posts: 21
The answer for Bill's question is -

1. The XML document is considerably big. Not really so big, to consider or cause performance hit issues. However, personally, I felt its good to go for a memory efficient way of doing it and its not a part of the requirement.
2. Yes, only replacement, updating, removal or addition of node values(and attribute values) is what is required.
3. Yes, the changes require the knowledge of the other XML contents in the document.

Paul - I could use an XSLT, I am not sure if it still would be good to use XSLT with a Swing based application?

Ulf - Ultimately, If I don't find a better API, I would eventually be using DOM or SAX

Thanks for all your replies and your time guys !
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18997
    
    8

Jade Jadaaih wrote:Paul - I could use an XSLT, I am not sure if it still would be good to use XSLT with a Swing based application?

Your choice of display technology is completely independent of how you choose to process your XML.
Jade Jadaaih
Greenhorn

Joined: Jun 17, 2008
Posts: 21
Paul Clapham wrote:
Your choice of display technology is completely independent of how you choose to process your XML.


So in that case, I will have to write an XSL for specific update of an XML, then pass the XSL along with XML to some API for update? Sorry to be dumb again Any specific APIs or code examples or web sites of how I could do this the XSL way you suggested?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12839
    
    5
3. Yes, the changes require the knowledge of the other XML contents in the document.


In that case the DOM approach will be easiest to code. There is no reason to go to other libraries, the standard Java library has all you need.

XPath makes for clarity of expression but at a huge cost in execution time.

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18997
    
    8

Jade Jadaaih wrote:So in that case, I will have to write an XSL for specific update of an XML, then pass the XSL along with XML to some API for update?


Yes, that's right. Just like you would have to write a Java program to do a specific update of an XML, if you decided to use DOM.

And no, I'm not going to direct you to a place on the Internet which has code snippets or tutorials about XSLT. I don't think that's the best way to start learning a language.
Jade Jadaaih
Greenhorn

Joined: Jun 17, 2008
Posts: 21
Paul Clapham wrote:
And no, I'm not going to direct you to a place on the Internet which has code snippets or tutorials about XSLT. I don't think that's the best way to start learning a language.


Thanks Paul !! I am happy I made a good guess on it

As one of the mentors around here, please, if you could just name me on the APIs I can proceed on from there Otherwise, I would have to again to find out them.
Jade Jadaaih
Greenhorn

Joined: Jun 17, 2008
Posts: 21
Paul, I found that XALAN is one way of doing it...

I am still trying to find more information...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18997
    
    8

Jade Jadaaih wrote:As one of the mentors around here, please, if you could just name me on the APIs I can proceed on from there


XSLT is built into Java. Go to the API documentation, the page which lists all the packages in the standard API, and scan it for "transform".
Jade Jadaaih
Greenhorn

Joined: Jun 17, 2008
Posts: 21
Thanks Paul...Will do that !!
Jade Jadaaih
Greenhorn

Joined: Jun 17, 2008
Posts: 21
Hi,

Back with couple of questions,

I have the following XML,


And, I would like to transform it into,


My XSL for the transformation is as follows,


My Java code for the transformation is as follows,


I am sure my XSL is wrong and I am unable to get the desired output. Which XSL tags should I look into?

Also, how do I pass a run time value to XSL during transformation? I found its done through <xsl:param/> some how it does not work.

Please let me where I am going wrong.

Thanks,
Jade
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18997
    
    8

<xsl:param> has to be a top-level element in your transformation. And look into how you use variables in XSLT.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best API to update an XML document!