I have an app that reads in the contents of file into a StringBuffer. It then reads the contents of another file which contains a list of certain words and phrases to replace in the original file. The list is in the following format: Word to replace;Replacing word I created this app to be able to speed up the replacement of xml tags in an xml document. (At my work, we're changing all the tags of our existing xml documents). So for example, the replacement list might look like this: <SingleSelect>;<SelectSingle> My app would go through the contents of the xml file, and every time it finds the phrase <SingleSelect> (including the angle brackets), it would replace it with <SelectSingle>. The problem is that some tags that need to be replaced are being named differently based on their position. For example: The original document:
::: Regex for Java A powerful, high-performance, regular expression library for Java. With Regex for Java, a search can be done for a string matching a regular expression pattern in your application. Regex for Java has been updated with a bug fix for the problem with Java 2 SDK 1.4. http://www.alphaWorks.ibm.com/tech/regex4j?open&l=LT030402,t=awfl
But I want to also be able to specify in the replacement list that the <Name> and </Name> tags should be replaced by <ElementName> and </ElementName>. I can't think of a way to get my app to do that advanced a search and replace. Do you know Thomas if Regex can do an advanced search and replace like that. Because in my scenario, I don't want all the <Name> and </Name> tags replaced...only the ones that are in a <TagOne>...</TagOne> structure. I guess in terms of specifying that in the replacement list, it shouldn't be too hard. I was thinking of using a colon to show it. For example: <TagOne>:<Name>;<ElementName> This might not be the best way, but that's a moot point. Please help if you can. Thanks. Hiran
Sounds to me like you need to parse the XML, not just do regular expression processing on it, since you are interested in the names of subnodes of particular nodes. So, in this case, you could use SAX, read in the old file and change names based on context while creating an output file, or you could use DOM and read in the whole tree, then go and rename nodes as appropriate and write out the results. In other words, you probably want to use JAXP: http://java.sun.com/xml/jaxp/index.html
Matt, I had that felling too first, but then I thought that he may need to have a "ParserCommander" class for each XML. So brute "find / replace" was more appropriated... You don't have the same feeling apparently ? Thomas,