We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27117
    • 28 Posts
    Hi

    I'm trying to parse a very simple xml document into a modx page. The xml file is stored under /assets/wetter/texte.xml.

    Now I want to display 2 nodes from this xml file in one of my modx pages.

    I tried to write a simplexml snippet but I have to say that I am not very familiar with php and all I get is a blank page when calling the snippet.

    I searched nearly 3 hours in the forum and on google but it seems that I am the only one with this problem.

    This is my snippet code:

    if (file_exists('assets/wetter/texte.xml')) {
       $xml = simplexml_load_file('assets/wetter/texte.xml');
    
    return = $xml->Tag[0];
    
    } else 
    {
       exit('Konnte Datei nicht laden.');
    }


    Can anyone point me in the right direction?

    Thanks
      Modx 2.1.3, PHP 5.2
      • 3749
      • 24,544 Posts
      Did you try doing a "View Source" on the page to see if there's anything there at all?

      You might also try this to see if there's an error in the retrieval or parsing:

      if($xml ===  FALSE) 
      { 
        return 'ERROR';
      } 
      [ed. note: BobRay last edited this post 14 years, 8 months ago.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 27117
        • 28 Posts
        Yes, I did that. But it's completely empty.

        But I changed the code to this:

        <?php
        
        if (file_exists('assets/wetter/texte.xml')) {
            $xml = simplexml_load_file('assets/wetter/texte.xml');
        
            print_r ($xml);
        } else {
            exit('Konnte texte.xml nicht öffnen.');
        }
        


        And now I get every node showed up one after another like this:

        SimpleXMLElement Object ( [@attributes] => Array ( [handle] => Meteo [type] => Text [generation_time] => 20120105024202 [last_update] => 20120104210319 ) [Bulletin] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Prognosen [prod] => prog [id] => 06631000 [language] => DE [type] => Region ) [Day] => SimpleXMLElement Object ( [@attributes] => Array ( [val] => 20120105 ) [Txt] => In der Nacht zuerst noch Wolkenlücken, dann bewölkt und gegen Morgen mit stark auffrischendem Südwestwind zunehmend Tropfen. Am Donnerstag stark bewölkt und immer wieder nass, dabei teils kräftiger Regen. Schneefallgrenze auf etwa 800 bis 1300 Metern. Temperaturen in Bern und Biel am Morgen 3 bis 4 Grad und am Nachmittag rund 6 Grad. Auf dem Gurten morgen bis 2 Grad. In der Nacht auffrischender Südwestwind. Am Donnerstag starker bis stürmischer Südwest- bis Westwind mit Böen im Flachland von 60 bis 80 km/h und in erhöhten Lagen über 100 km/h. Auf den Bergen morgen stürmischer Westwind mit Orkanböen, am Nachmittag auf Nordwest drehend. ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Aussichten [prod] => auss [id] => 06631000 [language] => DE [type] => Region ) [Day] => SimpleXMLElement Object ( [@attributes] => Array ( [val] => 20120105 ) [Txt] => Am Freitag im Seeland wechselnd bewölkt mit etwas Sonne und ein paar Schnee- oder Schneeregenschauern, im Oberland meist bewölkt und häufiger Schnee. Rund 3 Grad. Weiterhin windig. Am Samstag zunächst wechselnd bewölkt, gegen Abend vermehrt Wolken und zunehmend nass. 5 Grad. Am Sonntag im Seeland veränderlich mit ein paar Regengüssen, Richtung Alpen oft bewölkt und immer wieder Schnee oder in den tiefsten Lagen Regen. 4 Grad. ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Sporttipp [prod] => ssms [id] => 06631000 [language] => DE [type] => Region ) [Day] => SimpleXMLElement Object ( [@attributes] => Array ( [val] => 20120105 ) [Txt] => Am Donnerstag sind Indoorsportarten ganz klar zu bevorzugen, draussen ist es regnerisch und windig. ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Pollen [prod] => poll [id] => 06631000 [language] => DE [type] => Region ) [Day] => SimpleXMLElement Object ( [@attributes] => Array ( [val] => 20120105 ) [Txt] => // ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Luftschadstoffe [prod] => luft [id] => 06631000 [language] => DE [type] => Region ) [Day] => SimpleXMLElement Object ( [@attributes] => Array ( [val] => 20120105 ) [Txt] => // ) ) ) ) 


        But how can I get just a single node?
          Modx 2.1.3, PHP 5.2
          • 3749
          • 24,544 Posts
          Look at example #2 here: http://www.php.net/manual/en/simplexml.examples-basic.php.

          It won't work unless the XML is perfect, however.

          Feel free to post your XML, though it's probably correct if print_r() is showing at as an XML object. [ed. note: BobRay last edited this post 14 years, 8 months ago.]
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 27117
            • 28 Posts
            I saw the example #2 and I tried it like this. I just replaced echo with return but there was still no output.

            Maybe you see an error in the xml file.

            <?xml version="1.0" encoding="ISO-8859-1"?>
            <Article xml:lang="de" handle="Meteo" type="Text" generation_time="20120104214202" last_update="20120104210319" >
            	<Bulletin name="Prognosen" prod="prog" id="06631000" language="DE" type="Region">
            		<Day val="20120104">
            			<Txt>In der Nacht zuerst noch Wolkenlücken, dann bewölkt und gegen Morgen mit stark auffrischendem Südwestwind zunehmend Tropfen. Am Donnerstag stark bewölkt und immer wieder nass, dabei teils kräftiger Regen. Schneefallgrenze auf etwa 800 bis 1300 Metern. Temperaturen in Bern und Biel am Morgen 3 bis 4 Grad und am Nachmittag rund 6 Grad. Auf dem Gurten morgen bis 2 Grad. In der Nacht auffrischender Südwestwind. Am Donnerstag starker bis stürmischer Südwest- bis Westwind mit Böen im Flachland von 60 bis 80 km/h und in erhöhten Lagen über 100 km/h. Auf den Bergen morgen stürmischer Westwind mit Orkanböen, am Nachmittag auf Nordwest drehend.</Txt>
            		</Day>
            	</Bulletin>
            	<Bulletin name="Aussichten" prod="auss" id="06631000" language="DE" type="Region">
            		<Day val="20120104">
            			<Txt>Am Freitag im Seeland wechselnd bewölkt mit etwas Sonne und ein paar Schnee- oder Schneeregenschauern, im Oberland meist bewölkt und häufiger Schnee. Rund 3 Grad. Weiterhin windig. Am Samstag zunächst wechselnd bewölkt, gegen Abend vermehrt Wolken und zunehmend nass. 5 Grad. Am Sonntag im Seeland veränderlich mit ein paar Regengüssen, Richtung Alpen oft bewölkt und immer wieder Schnee oder in den tiefsten Lagen Regen. 4 Grad.</Txt>
            		</Day>
            	</Bulletin>
            	<Bulletin name="Sporttipp" prod="ssms" id="06631000" language="DE" type="Region">
            		<Day val="20120104">
            			<Txt>Am Donnerstag sind Indoorsportarten ganz klar zu bevorzugen, draussen ist es regnerisch und windig.</Txt>
            		</Day>
            	</Bulletin>
            	<Bulletin name="Pollen" prod="poll" id="06631000" language="DE" type="Region">
            		<Day val="20120104">
            			<Txt>//</Txt>
            		</Day>
            	</Bulletin>
            	<Bulletin name="Luftschadstoffe" prod="luft" id="06631000" language="DE" type="Region">
            		<Day val="20120104">
            			<Txt>//</Txt>
            		</Day>
            	</Bulletin>
            </Article>
            
              Modx 2.1.3, PHP 5.2
              • 27117
              • 28 Posts
              Thanks to the this
              http://www.electrictoolbox.com/php-simplexml-element-attributes/

              and this forum post
              http://www.phpbuilder.com/board/showthread.php?t=10334745


              I am now able to parse the xml file and read some attributes and contents out of it. This is the code:

              <?php
              
              $xml = simplexml_load_string(file_get_contents('assets/wetter/texte.xml'));
              $output = $xml->Bulletin[0]->attributes()->name;
              $output .=  $xml->Bulletin[0]->Day->Txt;
              return ($output);
              ?>
              
              
                Modx 2.1.3, PHP 5.2