aspose file tools*
The moose likes XML and Related Technologies and the fly likes converting xml tag elements 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 "converting xml tag elements" Watch "converting xml tag elements" New topic
Author

converting xml tag elements

manish ahuja
Ranch Hand

Joined: Oct 23, 2003
Posts: 312
Hi All

I have to communicate with an external application by sending a xml payload. This xml payload will adhere to the xsd schema given by the client.

The xml payload which I need to send to this external app actually comes in an xml format from another app (say app2).
On this we modify the xml a bit & plan to send it to the external app.

Now the payload which we recieve is more or less the same in terms of data values. The only problem being the xml tag elements.
The app2 sends me data in set of xml tags like say <app2_tag1>

Now I need to change these tags <app2_tag1> into something like <externalapp_tag1>. This is just an example the names could be widely different. This I would want to run on the entire xml document & convert into a format acceptable to the external app.

How can i acheive this where in I can keep the data intact & change the xml tags only, add a few new xml tags & delete a few tags from the xml received from app2.

Regards
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Sounds like a perfect candidate for XSLT transformation. Just apply a template to the incoming XML DOM to produce the DOM you want.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
manish ahuja
Ranch Hand

Joined: Oct 23, 2003
Posts: 312
Hi Paul

Thanks for the response. I saw a few XSLT examples but they are all transforming xml to html.

I want to see if possible an example of xml to xml & how to create and set the template.

Do post your suggestions

Regards
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

XSLT does not require you produce HTML. All it does is transform an XML document to something else based on a template. What the "something else" is is almost entirely up to you, and it could easily be another XML document. Remember HTML is just a mark up language, and that is all XML is too.
[ October 30, 2006: Message edited by: Paul Sturrock ]
manish ahuja
Ranch Hand

Joined: Oct 23, 2003
Posts: 312
Hi Paul

Thanks for your input. In the current scenario I am facing I have a huge dtd to confirm to. The xml which i will receive is only for parts of the dtd & will not have all elements specified in the dtd.
As I had mentioned in my earlier post I will have to convert the xml elements received from application1 to xml elements understandable to application2.

I guess whatever xml i get from app1 I will have to run through the xsl which will convert it xml acceptable to app2.

My question here is can I wrap this entire xsl functionality using Java. Something like what we do with XMLBeans. We get the incoming XML modify the xml using java methods as we have the java representation of the XSD to which this xml adheres to.

So can I wrap the xsl functionality in a java app & invoke methods for converting the xml into the prescribed format


Regards
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
So can I wrap the xsl functionality in a java app & invoke methods for converting the xml into the prescribed format


Yes

The standard Java library will let you run XSL transformations and a huge variety of other manipulations. You may have to do these changes in a series of operations, don't try to do it all at once.

If all you are doing is changing some tag names to fit a new namespace, adding and deleting a few tags, you may be able to do it with a SAX processing pass. The choice between doing everything with XSLT versus SAX depends on what you are more comfortable with.

Bill
manish ahuja
Ranch Hand

Joined: Oct 23, 2003
Posts: 312
Hi William

Thanks. Your reply was indeed very helpful & I guess we are now heading in the right direction. We do not have much of XML expertise.
As I have mentioned earlier I have a DTD to comply with. This DTD though huge it does not have any mandatory elements.

I would be getting chunks of XML payload which I need to convert for complying with the DTD. So I have to convert the xml (i.e. change the xml tags & add a few new ones) as well as validate if the converted xml is conforming with the DTD.
So say if i have <country> as a root element comprising of lots of child elements like <geography> , <cities> etc at one instance i will only get <cities> where i will have to change the tag itself & send.

The approaches you mentioned (XSL vs SAX) sounds apt. But can I get to see a sample link where in we can change the incoming xml code to totally new set of tags & we have a new xml code.
I am trying to find some java code which can give us a headstart in this direction.

Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: converting xml tag elements