Skip to main content
February 4, 2008
Question

Which is the best way to parse nested XML

  • February 4, 2008
  • 3 replies
  • 466 views


Hello, I am just getting back into using ColdFusion and I am working on a project that needs to parse some XML.

The XML is generated by a third party document.

I figured out how to grab individual values and such, but I'm not sure what is the best way to grab a value in relation to where it is in the XML.

Following is a rather simplified example of the XML

I need to grab the value of 'Rich Text' in relation to the 'ID name' value.

<project>
<CT text>
<Group>
<id name = text1>
<Rich Text>some sample text</Rich Text>
</id>
</Group>
</CT Text>
<CT text>
<Group>
<Group>
<id name = text1>
<Rich Text>some sample text</Rich Text>
</id>
</Group>
</Group>
</CT Text>
<CT text>
<id name = text2>
<Rich Text>some more sample text</Rich Text>
</id>
</CT Text>
</project>

Once parsed, the info I need to output on the page would look like
text 1
some sample text

text 2
some more sample text

The part that makes this difficult for me is that there may be 0 'Group' values or there might be a number of them. As such, I can't just grab the value by listing the tree out, since it changes depending on the document (there are thousands of these documents that I need to parse).

From what I have seen in some XML examples, there are a number of ways to go about doing this, but before I put a lot of time into any of them, I was wondering what the 'best' way to do this is.

XSLT? XMLSearch? Structures? Some combination of those or other functions?

P.S. what little I know of using ColdFusion with XML all comes from the doc written by Nate Weiss.

I was a bit let down that after spending well over $1,000 for ColdFusion, Adobe left out the only two books that were worth printing (the CFML reference guide and the Advanced book). It wouldn't have bothered me as much if there was something on the product page listing the manuals that said 'we only actually give you 2 of the ones listed'.


Best regards
Kevin R.
    This topic has been closed for replies.

    3 replies

    February 8, 2008
    Hello,

    Could someone do me a huge favor and provide some sample code that would parse/output the above xml so that the output would look something like the following. . .

    text 1 = some sample text

    text 2 = some more sample text

    The problem I am having is displaying the xmltext for the RICH TEXT element in relation to the ID element.

    The code below will return the value of the RICHTEXT (incorrectly named in the original XML examples) easily enough, but I need to have it show which text number it is.

    Best regards,
    Kevin R.
    Inspiring
    February 5, 2008
    > I was wondering what the 'best' way to do this is. XSLT? XMLSearch?
    > Structures?

    There is no "best way" to effect your specific requirements, really.
    Leveraging any of the above techniques would be fine. It just depends on
    what you're most comfortable with.

    Personally, XSL does my head in a bit, so I stick with xmlSearch() and
    looping over the results. I do find that direct access to the XML document
    with atruct/array notation is a bit clunky, but that's just me.

    --
    Adam
    February 4, 2008
    Sorry about the first post. The preview of the post had the less than / greater than characters displayed correctly, so I went ahead and posted it.

    I will re-post it below.