开发者

Java replace in XML-file

I have created my own XML-file on my Android phone, which looks similar to this

<?xml version="1.0" encoding="utf-8" ?> 
<backlogs> 
   <issue id="1"> 
      <backlog id="0" name="Linux" swid="100" /> 
      <backlog id="0" name="Project Management" swid="101" /> 
   </issue> 
   <issue id="2"> 
      <backlog id="0" name="Tests" swid="110" /> 
      <backlog id="0" name="Online test" swid="111" /> 
      <backlog id="0" name="Test build" swid="112" /> 
      <backlog id="0" name="Update" swid="113" /> 
   </issue> 
</backlogs>

I have then converted it into a String to replace inside the string using Regular Expression, but I have a problem with the Regular Expression.

The Regular Expression I just created looks like this

([\n\r]*)<(.*)issue(.*)1(.*)([\n\r]*)(.*)([\n\r]*)(.*)([\n\r]*)(.*)<(.*)/(.*)issue(.*)

I need to replace the specific issue-tag (located with the specific ID) with another issue-tag in another String

The Regular Expressi开发者_C百科on works fine for the tag with ID 1, but not with ID 2 as there is another amount of tags, but is there any way to get around the use of amount?

I hope you understand my question

I finally found a solution for my question, which is

([\n\r]*)<(.*)issue(.*)1[\S\s]*?<(.*)/(.*)issue(.*)


Do not use regex. Please. Use XML parser.

Do you know what is the highest voted SO answer


Use a SAX (or StAX) parser and writer at the same time.

As you read one event, detect whether to write the same event type to the writer without modification, or to do some modifications in the state you are currently in - like swapping an element name or attribute value. This will handle an unlimited amount of elements at the expense of CPU usage; in general it will be pretty light-weight.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜