Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression or else?  RSS feed

 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a input xml and want to generate another output xml taking this xml as an input.
I am trying this approch:
1) I parse the input xml and generate a object of it.
2) I create a template of output xml needed.
3) I parse the template xml and generate the string represntation of the xml.
4) I use Pattern and Matcher to replace the template node values with actual ones.

I have a template xml, with value of nodes substitued as
<name>${name}</name>
I parsed this template xml in java and want to replace all occurances of ${name} based on the object generated from input xml

When I use this in my java class:
Pattern p = Pattern.compile("${name}");
Matcher m = p.matcher(strTemplate); // strTemplate has d template XML parsed
strReturn = m.replaceAll("John");

I am getting the follwing error:
java.util.regex.PatternSyntaxException: Illegal repetition near index 0
${name}

Can you help me in replacing the string ${name} with say John

Also heard using regular expression in a loop or more times is a serious perfomance hit. Can you let me know a better and efficient way to convert one form of xml to other?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Processing XML with regexps is fraught with problems. Is there a particular reason you don't want to use more standard APIs like JAXP/DOM/XPath for the task?
[ June 01, 2007: Message edited by: Ulf Dittmer ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that mucking about with tags and attributes is tough in raw String format, but a simple macro replacement ${name} should be ok. Here's a regex I use for exactly that macro pattern. My application allows nested macros so I have to create a new matcher after every replacement

I haven't profiled the performance of this loop because the whole app is satisfactory.
 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So wat would be the best way(performance wise) to transform one xml to another. any piece of code wud b highly appreciated.
 
Vyas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use JAXP or other Java parser libraries
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant and Vyas-

You may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since neither of yours conforms with it, please take a moment to change it, which you can do right here.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to consider using an existing template engine. We've made good experiences with FreeMarker: http://freemarker.sourceforge.net/
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!