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.
    – Matt Ball
    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? Jun 1, 2011 at 16:10
  • 1
    mmm, maybe XSLT could help you?
    – ignis
    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
    – The87Boy
    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

4
  • How can I use XML parser to replace?
    – The87Boy
    Jun 1, 2011 at 16:11
  • parse the xml. make in memory changes. then write back if u want Jun 1, 2011 at 16:13
  • 5.000 objects is a lot, so I don't think the Android's memory can handle that
    – The87Boy
    Jun 1, 2011 at 19:13
  • @The87Boy Thats why we have SAX parsers :) But I am sure andorid will have a better replacement Jun 2, 2011 at 6:01
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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.