0

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 Expression 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(.*)
4
  • 2
    For the ∞th time, don't use a regex to work with non-regular languages. Use a parser. Commented Jun 1, 2011 at 16:10
  • Have you thought about parsing it into an XML Document object making your changes like that instead using regex's and strings? Commented Jun 1, 2011 at 16:10
  • 1
    mmm, maybe XSLT could help you? Commented Jun 1, 2011 at 16:47
  • I have not thougt about XML Document object, but about Java Objects and then write them, but I think that 5.000 objects is a lot for Android Commented Jun 1, 2011 at 18:10

2 Answers 2

3

Do not use regex. Please. Use XML parser.

Do you know what is the highest voted SO answer

Sign up to request clarification or add additional context in comments.

4 Comments

How can I use XML parser to replace?
parse the xml. make in memory changes. then write back if u want
5.000 objects is a lot, so I don't think the Android's memory can handle that
@The87Boy Thats why we have SAX parsers :) But I am sure andorid will have a better replacement
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.