How to find non-existing nodes or nodes with no text.

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

    How to find non-existing nodes or nodes with no text.

    Ok, this must be simple but the more i search the more i don't find.
    It's about SimpleXML and PHP.
    How to find non-existing nodes or nodes with no text

    My XML file looks a little bit like this:

    i.e. 1
    <discography>
    <CD>
    <title></title>
    <year>1978</year>
    </CD>
    </discography>


    i.e. 2:
    <discography>
    <CD>
    <year>1978</year>
    </CD>
    </discography>


    In i.e. 1, the tag <titleis empty. How do i test for empty tags in
    PHP?
    In i.e. 2, the tag <titledoesn't even exist. How do i test for non-
    existing tags?


    This is how i get the values from XML in PHP without validation
    ....
    //set the XML file name as a PHP string
    $cd_xml = "db/discography/cd.xml";

    //load the XML file
    $xml = @simplexml_load _file($cd_xml) or die ("Error: The XML file
    can't open.") ;
    ....

    //no validation if empty, creates empty <h1tags. Not good!
    <h1><?php echo $xml->CD->title; ?></h1>
    <h2><?php echo $xml->CD->year; ?></h2>





    How to validate?
    Any suggestions?

    Thanks in advance
    Marco
  • Jerry Stuckle

    #2
    Re: How to find non-existing nodes or nodes with no text.

    SM wrote:
    Ok, this must be simple but the more i search the more i don't find.
    It's about SimpleXML and PHP.
    How to find non-existing nodes or nodes with no text
    >
    My XML file looks a little bit like this:
    >
    i.e. 1
    <discography>
    <CD>
    <title></title>
    <year>1978</year>
    </CD>
    </discography>
    >
    >
    i.e. 2:
    <discography>
    <CD>
    <year>1978</year>
    </CD>
    </discography>
    >
    >
    In i.e. 1, the tag <titleis empty. How do i test for empty tags in
    PHP?
    In i.e. 2, the tag <titledoesn't even exist. How do i test for non-
    existing tags?
    >
    >
    This is how i get the values from XML in PHP without validation
    ...
    //set the XML file name as a PHP string
    $cd_xml = "db/discography/cd.xml";
    >
    //load the XML file
    $xml = @simplexml_load _file($cd_xml) or die ("Error: The XML file
    can't open.") ;
    ...
    >
    //no validation if empty, creates empty <h1tags. Not good!
    <h1><?php echo $xml->CD->title; ?></h1>
    <h2><?php echo $xml->CD->year; ?></h2>
    >
    >
    >
    >
    >
    How to validate?
    Any suggestions?
    >
    Thanks in advance
    Marco
    >
    Have you tried RTFM? A string with no characters is just that - a
    zero-length string.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • SM

      #3
      Re: How to find non-existing nodes or nodes with no text.

      On May 9, 8:54 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
      SM wrote:
      Ok, this must be simple but the more i search the more i don't find.
      It's about SimpleXML and PHP.
      How to find non-existing nodes or nodes with no text
      >
      My XML file looks a little bit like this:
      >
      i.e. 1
      <discography>
      <CD>
      <title></title>
      <year>1978</year>
      </CD>
      </discography>
      >
      i.e. 2:
      <discography>
      <CD>
      <year>1978</year>
      </CD>
      </discography>
      >
      In i.e. 1, the tag <titleis empty. How do i test for empty tags in
      PHP?
      In i.e. 2, the tag <titledoesn't even exist. How do i test for non-
      existing tags?
      >
      This is how i get the values from XML in PHP without validation
      ...
      //set the XML file name as a PHP string
      $cd_xml = "db/discography/cd.xml";
      >
      //load the XML file
      $xml = @simplexml_load _file($cd_xml) or die ("Error: The XML file
      can't open.") ;
      ...
      >
      //no validation if empty, creates empty <h1tags. Not good!
      <h1><?php echo $xml->CD->title; ?></h1>
      <h2><?php echo $xml->CD->year; ?></h2>
      >
      How to validate?
      Any suggestions?
      >
      Thanks in advance
      Marco
      >
      Have you tried RTFM? A string with no characters is just that - a
      zero-length string.
      >
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstuck...@attgl obal.net
      =============== ===
      ok thanks FM, i will try the RTFM
      Marco

      Comment

      Working...