0

I have an XML file that is aranged like so:

<xml:head>
    <xml:reportObject>
        <xml:device>
            <device:id>
                <id:value = value />
            </device:id>
            <device:OpAttributes>
                <OpAttributes:value = value />
            <device:OpAttributes>
            <device:Config>
                <Config:NetConfig>
                    <NetIF:ID = value />
                    <NetHost>
                        <NetHost:MAC = value />
                    </NetHost>
                </Config:NetConfig>
            </device:Config>
            <device:Role = value />
            <device:TaggedString name="value" value="value" />
            <device:Addition junk ........ />
        </xml:device>
    </xml:reportObject>
    Lather, Rinse, Repeat for several instances on reportObjects
</xml:head>

My problem is that I am trying to parse out three values (specifically the "NetHost:MAC", "device:Role" and the "device:TaggedString" values) to dump into their place in a database column.

The program we use is an in-house tool that will do this based on RegEx matches, but because the XML flatlines after the "xml:device" tag, I am left searching for a way to match everying withing the "xml:device" tags to continue further parsing... the kicker is that I can only continue to parse if the "device:Role" tag is a client. Anything else gives too much junk and my parsing bombs.

My most recient attempt (and subsequent failure) to do this looks like this:

<xml:device([\s\S]+?(\b\w*Client\w*\b))</xml:device>

This works for 90% of my matches, but somewhere within the file, the [\s\S]+? is matching too far down, due to lack of an earlier match, and still making my parsing bomb.

Any help will keep me from pulling the rest of my hair our.

RegEx is the only option I have to do this parsing at the moment via our in-house tool. If you can think of something different, please let me know.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • 8
    "My problem is that I am trying to parse out three values" - No. Your problem is that you are trying to parse XML using regexes while you can have a nice `NSXMLParser` instance... –  Feb 11 '13 at 21:41
  • ... and that your XML seems to be missing some closing tags (e.g. ``) – crackmigg Feb 11 '13 at 21:42
  • Sorry, too hair pulling had me forget to add the "/"'s in my closing tags. I have edited it accordingly. I wish the problem were that easy. – Jordan Gregory Feb 11 '13 at 21:44
  • 1
    [Don't use regexes](http://stackoverflow.com/a/1732454) – Kevin Feb 11 '13 at 21:47
  • 1
    If the only option you have is regex _using an already written program_, then not only is this not an Objective-C question, it's not even programming. This would be more appropriate for [SU]. – jscs Feb 11 '13 at 22:32

1 Answers1

2

Instead of pulling your hair out (girls don't like bald programmers) while trying to use regular expressions, try out the NSXMLParser class (which is nicely documented by Apple). It will be much easier to set up for this task.

  • Unfortunately, I mistagged the question, this is not an NS related item. My apologies. But unfortunately, RegEx is my only option in this particular instance. – Jordan Gregory Feb 11 '13 at 21:50
  • 1
    @JordanGregory `objective-c`... well... Please explain why you **can't** use anything other than reguar expressions. –  Feb 11 '13 at 21:51
  • 1
    You should not use regex, but if you have to for whatever reason, try to use `([\s\S]+?)` as regex and parse for your client role afterwards. Dont try to do this part with a regex at least! – crackmigg Feb 11 '13 at 21:54
  • We are very limited on what programs we are allowed to install (thus, we have a few in-house devs that put some things together for us). The tool we use takes snips of a structured file, and continues parsing on those snips until you get what you desire, but in this case, after the file flatlines, it becomes difficult. – Jordan Gregory Feb 11 '13 at 21:55
  • 1
    @JordanGregory The nice thing is that you don't have to install anything to access `NSXMLParser`. It's in the system by default (part of the Foundation framework on both OS X and iOS). –  Feb 11 '13 at 21:55