aspose file tools
The moose likes Beginning Java and the fly likes string manipuation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "string manipuation" Watch "string manipuation" New topic
Author

string manipuation

Mohit Sinha
Ranch Hand

Joined: Nov 29, 2004
Posts: 125
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

Joined: Apr 14, 2004
Posts: 10336

Not sure I follow what you are trying to do. Can you post a before and after example of the XML?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mohit Sinha
Ranch Hand

Joined: Nov 29, 2004
Posts: 125
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

Joined: Dec 13, 2006
Posts: 5
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)

=)


SCJA, SCJP 5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16680
    
  19

This can be easily done with regular expressions... Try...



Henry
[ December 20, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mohit Sinha
Ranch Hand

Joined: Nov 29, 2004
Posts: 125
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

Joined: Sep 28, 2004
Posts: 16680
    
  19

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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: string manipuation
 
Similar Threads
Package creation
Dtd generation
Very interesting point in inheritance
Confusion in JAXP DOM parser
Find Level of Element in XML