simpleXML and PHP. How to find if a node is empty or exists?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SM

    simpleXML and PHP. How to find if a node is empty or exists?

    I think that simpleXML is the best thing i've seen in PHP5. It's so
    simple (d'ho!) and easy to use when

    it comes to reading a simple XML file. The only thing i'm not sure how
    to achieve is finding if an XML

    node is empty or if the node exist or doesn't exist.

    I'm searching the web, but got nothing yet. I was thinking of posting
    it here.

    This is a condensed version of the code:

    <?xml version="1.0" encoding="UTF-8"?>
    <CD>
    <title>Shine</title>
    <year>1985</year>
    <label></label>
    <description>Go od CD from the 1980s</description>

    <item>
    <track id="18">Moonlig ht</track>
    <track>Shine</track>
    <track>Absolute </track>
    </item>

    <item>
    <track>Relative </track>
    <track>Star</track>
    <track>Crazy</track>
    </item>
    </CD>



    $file = 'db_cd_number.x ml';
    $xml = @simplexml_load _file($file);



    //??????
    if(title node is empty || title node doesn't exist) {
    echo 'title is empty or doesnt exist, do something';
    } else {
    echo 'title OK, continue';
    }



    //i.e. 2 What about attributes????? ?????
    foreach ($xml->item-as $item) {
    foreach($item->track as $track) {
    if($track['id'] doesn't exist or empty) {
    echo 'dont show the lyrics';
    }
    }
    }


    How do i find if a node is empty or if it exists?


    Thanks
    Marco
  • Derik

    #2
    Re: simpleXML and PHP. How to find if a node is empty or exists?

    How do i find if a node is empty or if it exists?




    I made some changes- I inverted your if-tests so that they're testing
    for positives not negatives (it's more intuitive to write, you can use
    your !'s if you really need 'em that way for some reason.)
    (drawing from string not file for simplicity)

    <?php

    $string = '<?xml version="1.0" encoding="UTF-8"?>
    <CD>
    <title>Shine</title>
    <year>1985</year>
    <label></label>
    <description>Go od CD from the 1980s</description>

    <item>
    <track id="18">Moonlig ht</track>
    <track>Shine</track>
    <track>Absolute </track>
    </item>

    <item>
    <track id="">Relative </track>
    <track id="4">Star</track>
    <track id="0">Crazy</track>
    </item>
    </CD>';

    $xml = @simplexml_load _string($string );

    if( $xml->title && ($xml->title != '') ) {
    echo 'title OK, continue<br/><br/>';
    } else {
    echo 'title is empty or doesnt exist, do something<br/><br/>';
    }

    foreach ($xml->item as $item) {
    foreach($item->track as $track) {
    $attribs = $track->attributes() ; //You have to

    if( $attribs['id'] && $attribs['id'] == true) {
    echo '<b>Lyrics:</b<i>"Oh baby ' . $track . '- yeah, my honey
    baby ' . $track . '~"</i><br/>';
    } else{
    echo 'LYRICS FOR "' . strtoupper($tra ck) . '" WILL NOT NOT
    SHOWN<br/>';
    }
    }

    }
    ?>
    This should work as long as your ID's are greater than 0. <title>
    works even if the title is the number 0 or the string 'false'.

    The .txt file I linked to above has comments with alternate
    approaches.

    -Derik

    Comment

    • SM

      #3
      Re: simpleXML and PHP. How to find if a node is empty or exists?

      On May 27, 9:15 pm, Derik <ReGenes...@aol .comwrote:
      How do i find if a node is empty or if it exists?
      >

      >
      I made some changes- I inverted your if-tests so that they're testing
      for positives not negatives (it's more intuitive to write, you can use
      your !'s if you really need 'em that way for some reason.)
      (drawing from string not file for simplicity)
      >
      <?php
      >
      $string = '<?xml version="1.0" encoding="UTF-8"?>
      <CD>
      <title>Shine</title>
      <year>1985</year>
      <label></label>
      <description>Go od CD from the 1980s</description>
      >
      <item>
      <track id="18">Moonlig ht</track>
      <track>Shine</track>
      <track>Absolute </track>
      </item>
      >
      <item>
      <track id="">Relative </track>
      <track id="4">Star</track>
      <track id="0">Crazy</track>
      </item>
      </CD>';
      >
      $xml = @simplexml_load _string($string );
      >
      if( $xml->title && ($xml->title != '') ) {
      echo 'title OK, continue<br/><br/>';} else {
      >
      echo 'title is empty or doesnt exist, do something<br/><br/>';
      >
      }
      >
      foreach ($xml->item as $item) {
      foreach($item->track as $track) {
      $attribs = $track->attributes() ; //You have to
      >
      if( $attribs['id'] && $attribs['id'] == true) {
      echo '<b>Lyrics:</b<i>"Oh baby ' . $track . '- yeah, my honey
      baby ' . $track . '~"</i><br/>';
      } else{
      echo 'LYRICS FOR "' . strtoupper($tra ck) . '" WILL NOT NOT
      SHOWN<br/>';
      }
      }
      >
      }
      >
      ?>
      This should work as long as your ID's are greater than 0. <title>
      works even if the title is the number 0 or the string 'false'.
      >
      The .txt file I linked to above has comments with alternate
      approaches.
      >
      -Derik
      Just one question: Why do you use this twice : if( $attribs['id'] &&
      $attribs['id'] == true). Is this a typo or it should be called twice?

      Thanks

      Comment

      • SM

        #4
        Re: simpleXML and PHP. How to find if a node is empty or exists?

        On May 27, 9:15 pm, Derik <ReGenes...@aol .comwrote:
        How do i find if a node is empty or if it exists?
        >

        >
        I made some changes- I inverted your if-tests so that they're testing
        for positives not negatives (it's more intuitive to write, you can use
        your !'s if you really need 'em that way for some reason.)
        (drawing from string not file for simplicity)
        >
        <?php
        >
        $string = '<?xml version="1.0" encoding="UTF-8"?>
        <CD>
        <title>Shine</title>
        <year>1985</year>
        <label></label>
        <description>Go od CD from the 1980s</description>
        >
        <item>
        <track id="18">Moonlig ht</track>
        <track>Shine</track>
        <track>Absolute </track>
        </item>
        >
        <item>
        <track id="">Relative </track>
        <track id="4">Star</track>
        <track id="0">Crazy</track>
        </item>
        </CD>';
        >
        $xml = @simplexml_load _string($string );
        >
        if( $xml->title && ($xml->title != '') ) {
        echo 'title OK, continue<br/><br/>';} else {
        >
        echo 'title is empty or doesnt exist, do something<br/><br/>';
        >
        }
        >
        foreach ($xml->item as $item) {
        foreach($item->track as $track) {
        $attribs = $track->attributes() ; //You have to
        >
        if( $attribs['id'] && $attribs['id'] == true) {
        echo '<b>Lyrics:</b<i>"Oh baby ' . $track . '- yeah, my honey
        baby ' . $track . '~"</i><br/>';
        } else{
        echo 'LYRICS FOR "' . strtoupper($tra ck) . '" WILL NOT NOT
        SHOWN<br/>';
        }
        }
        >
        }
        >
        ?>
        This should work as long as your ID's are greater than 0. <title>
        works even if the title is the number 0 or the string 'false'.
        >
        The .txt file I linked to above has comments with alternate
        approaches.
        >
        -Derik
        Hey thanks Derik.
        This is what i've been looking for.

        Thanks again
        Marco

        Comment

        Working...