Big XML Mess (help)

Q&A's, tips, howto's
Locked
kanen
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm
Contact:

Big XML Mess (help)

Post by kanen »

newLISP experts,

I'm having real problems finding data in the CAPEC XML document. ( http://capec.mitre.org/data/xml/capec_v1.5.xml )

I've read through everything on the forums and I've tried a bunch of things, but I seem to only be able to dig into the XML if I know what field I'm looking for... and, even then, it's a nightmare to parse.

I feel like I'm missing some obvious way to do a lookup, but I just can't get there...

My code:

Code: Select all

(context 'Xml)
(define (Parse xml-string)
  (xml-type-tags nil nil nil nil)
  (set 'rtn-xml (xml-parse xml-string (+ 1 2 4) 'CAPEC)) )

(set 'capec-file (read-file "http://capec.mitre.org/data/xml/capec_v1.5.xml"))
(set 'capec-xml (read-file capec-file))
(set 'capec (Xml:Parse capec-xml))
I can run:

Code: Select all

(println (lookup "Attack_Pattern_Catalog" capec))
... and I get the whole XML structure as a big fat list.

Now, If I run:

Code: Select all

(set 'capec-categories (lookup "Attack_Pattern_Catalog" capec 2))
(println capec-categories)
I get some XML that seems to make sense, but I still need to be able to reference everything properly. The CAPEC XML structure is just a nested freaking nightmare (as you can see above).

Is there any way to grab one of the items using (lookup <item-name> capec) or is there something else I need to start thinking about? Is there a better way to structure the XML into an assoc or list so I can walk through it or do a lookup in a meaningful way?

Lutz commented to me and said,
Lutz wrote:'lookup' is for one-dimensional flat association lists. Use 'ref' or 'ref-all' they search through deeper nested lists and return and index vector of the element found.

You can then 'chop' off the last index or two to get the bigger enclosing association list.

Start with a smaller example to see how it works.
So, my question is, "Has anyone dealt with XML this complicated (and nested) and do you have an example of how to interact with that XML in newLISP?"

I'd love to see some examples that work better than what I'm dealing with right now... :)
. Kanen Flowers http://kanen.me .

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Big XML Mess (help)

Post by cormullion »

Yes, it can be a nightmare navigating those sxml lists...

I've written a bit about it here: http://en.wikibooks.org/wiki/Introducti ... g_with_XML and here http://unbalanced-parentheses.nfshost.com/listlistolist and here http://unbalanced-parentheses.nfshost.c ... stindexing...

There are lots of other techniques to try - using ref with match-style wild-cards, for example.

Also - http://newlispfanclub.alh.net/forum/vie ... =16&t=3624 was a recent discussion...



...somewhat in haste... :)

Locked