Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / May 2006



Tip: Looking for answers? Try searching our database.

Simple XML (XPATH) 'nested loop' possible?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
zu - 30 May 2006 20:38 GMT
What's the best way to output the entries of an XML file with the following
structure as a nested list  (actually it needs to become a form where all
entries can be checked).

I can output each level (see code) but it seems not to be possible to nest the
for-loops.

Any hints if it's possible with XPath directly or do I need to save the result
of one loop to output within the other or would it even be better to make a
query out of the XML result?

<menues>
    <menu name="Essen">
        <eintrag name="Fisch" aktion="message" variables="Fisch" nr="1001"/>
        <eintrag name="Fleisch" aktion="message" variables="Fleisch" nr="1002"/>
        <eintrag name="Fr?hst?ck" aktion="message" variables="Fr?hst?ck" nr="1010"/>
        <eintrag name="Babynahrung" aktion="message" variables="Babynahrung"
nr="1012"/>
        <eintrag name="Anderes" aktion="message" variables="Essen, anderes"
nr="1013"/>
    </menu>
    <menu name="Trinken">
        <eintrag name="S?fte" aktion="message" variables="S?fte" nr="2004"/>
        <eintrag name="Sirup" aktion="message" variables="Sirup" nr="2006"/>
        <eintrag name="Aperitif" aktion="message" variables="Aperitif" nr="2008"/>
    </menu>
    <menu name="Haushalt">
        <eintrag name="Putzen" aktion="message" variables="Putzen" nr="3001"/>
        <eintrag name="Garten" aktion="message" variables="Garten" nr="3003"/>
        <eintrag name="Blumen" aktion="message" variables="Blumen" nr="3004"/>
    </menu>

<cfscript>
    myxmldoc = XmlParse(myxml);

   
    selectedElements = XmlSearch(myxmldoc, "/menues/menu");
    for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)
        writeoutput("<b>" & selectedElements[i].XmlAttributes.name & "</b><br>" );
       
        selectedSubElements = XmlSearch(myxmldoc, "/menues/menu/eintrag");
        for (i = 1; i LTE ArrayLen(selectedSubElements); i = i + 1)
            writeoutput(selectedSubElements[i].XmlAttributes.name & "<br>");
</cfscript>
BKBK - 31 May 2006 11:53 GMT
Make use of the fact that you may interprete an XML element as an array.

<cfxml variable="myxml" casesensitive="no">
<menues>
    <menu name="Essen">
        <eintrag name="Fisch" aktion="message" variables="Fisch" nr="1001"/>
        <eintrag name="Fleisch" aktion="message" variables="Fleisch" nr="1002"/>
        <eintrag name="Fr?hst?ck" aktion="message" variables="Fr?hst?ck" nr="1010"/>
        <eintrag name="Babynahrung" aktion="message" variables="Babynahrung"
nr="1012"/>
        <eintrag name="Anderes" aktion="message" variables="Essen, anderes"
nr="1013"/>
    </menu>
    <menu name="Trinken">
        <eintrag name="S?fte" aktion="message" variables="S?fte" nr="2004"/>
        <eintrag name="Sirup" aktion="message" variables="Sirup" nr="2006"/>
        <eintrag name="Aperitif" aktion="message" variables="Aperitif" nr="2008"/>
    </menu>
    <menu name="Haushalt">
        <eintrag name="Putzen" aktion="message" variables="Putzen" nr="3001"/>
        <eintrag name="Garten" aktion="message" variables="Garten" nr="3003"/>
        <eintrag name="Blumen" aktion="message" variables="Blumen" nr="3004"/>
    </menu>
</menues>
</cfxml>
<cfscript>
    n = 1;
    myxmldoc = XmlParse(myxml);
    selectedElements = XmlSearch(myxmldoc, "/menues/menu");
    for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1) {
        writeoutput("<b>" & selectedElements[i].XmlAttributes.name & "</b><br>" );
        selectedSubElements = XmlSearch(myxmldoc, "/menues/menu[#n#]/eintrag");
        for (j = 1; j LTE ArrayLen(selectedSubElements); j = j + 1)
            writeoutput(selectedSubElements[j].XmlAttributes.name & "<br>");
        n = n + 1;writeoutput("<br>");
    }
</cfscript>
zu - 31 May 2006 16:54 GMT
Thanks, great!

I tried it somwhere along this before but it didn't work then. I propably just
forgot to put the pound signes to the correct place or something ;-)

But yes, your solution is exactly what I was looking for!

Thank you
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.