• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Regular Expression or else?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use JAXP or other Java parser libraries
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
No, tomorrow we rule the world! With this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic