Here is what i am trying to do Since I am getting the data as list of item values in a String array.
My code is something like this
Pattern pat = Pattern.compile("/"); String strs = pat.split("/Book/subject/Fiction");
for (int i = 0; i < strs.length; i++) System.out.println("Next token: " + strs[i]);
The only catch I have currently is generating the dynamic xml element tag names based on the value of these token. i.e. first token = top level xml element subsequent tokens = child xml elements last token = value in the last child xml element
You mentioned about replaceFirst. I tried looking into the same. Can you ellaborate a bit on the same.
The only catch I have currently is generating the dynamic xml element tag names based on the value of these token.
What's the catch? You already parsed the tokens into members of the string array -- building an XML string based on the tokens is relatively easy.
As for replaceFirst(), I was thinking about a fixed number of tokens -- your question had only three. In that regard, what you are doing should work better, but as a FYI, here is my solution (for only 3 tokens)...
For a variable number of levels, regular expressions don't work very well - if at all.
I would probably parse each line using String.split, and then build the XML tree using DOM (or preferably Dom4J).
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
That was a neat way to acheive the same. But in my case that fails as I don't have a fixed number of tokens.
Can you give us some more hints.
What more hints do you need? You acknowledged that you got all the tokens via the split() method. You just have to build the XML string -- which can be accomplished with a loop, writing to a string buffer.
Originally posted by manish ahuja: Yeah thats what we are doing. From your earlier post we thought you are hinting at some advanced Regex pattern functions.
Well, I guess you could use a combination of find(), appendReplacement(), and appendTail(), to do the regex search and xml generation in a single pass, instead of having a separate passes for split() and the generation of the xml, but...
Considering that you have only 3 to 5 tokens in the path, it shouldn't make any difference. What you are doing should work well.