• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

converting xml tag elements

 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like a perfect candidate for XSLT transformation. Just apply a template to the incoming XML DOM to produce the DOM you want.
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
The only cure for that is hours of television radiation. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic