Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

string manipuation  RSS feed

 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I am receiving a String content from a method which is a chunk of xml.
The xml is something like this
<parent1>
<child>blahblah</child>
other tags
</parent1>

Or i can recive some thing like this

<parent2>
<child1>blahblah</child1>
other tags
</parent2>


The input xml i recieve will vary dynamically. What i want to do is interspere a fe xml tags just before the closing tag (</parent1> or </parent2> in this case.

Can someone tell me what would be a neat way of doing the same


Regards
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure I follow what you are trying to do. Can you post a before and after example of the XML?
 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Thanks for the response.

Currently I have to write a method which gets a xml payload. We will not be actually using xml parsing & validation here.

So I have to interspere an xml tag inside the xml payload which i receive as input.

So say I have the before xml something like this
****************
<parent1>
<child>blahblah</child>
</parent1>
****************
The after xml should be something like this

****************
<parent1>
<child>blahblah</child>
<newtag>content</newtag>
</parent1>
****************

I have to acheive this via string manipulation.

Regards,
 
Wanderlei C. A. Souza
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recommend some XML framework, it will make the things easier.

Try Jakarta Betwixt (http://jakarta.apache.org/commons/betwixt/)
or Castor (http://www.castor.org/xml-framework.html)

=)
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This can be easily done with regular expressions... Try...



Henry
[ December 20, 2006: Message edited by: Henry Wong ]
 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thats neat.

If you can just ellaborate what this does it would give us a good understanding on the same.


Thanks & Regards,
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mohit Sinha:
If you can just ellaborate what this does it would give us a good understanding on the same.

Thanks & Regards,


It just does a regex match on XML -- and capturing it in two groups. For the XML match, it confirms that it must start with a "<tag>" and end with a "</tag>". And that the "tag" that is used is the same, this is done with another group.

Once the match is established, it will insert the new XML between the two captured groups.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!