Can $node->node_value() return a null value?

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

    #1

    Can $node->node_value() return a null value?

    Hi,

    I am using the following function to access a nodes value from an XML
    file. It works fine until it discovers a empty tag. I would like to
    have the option of leaving some tags empty for my script.


    ..
    ..
    ..
    function foutput_contact ($node_name) {
    $xml_document = domxml_open_fil e('xml/contacts.xml');
    if ($xml_document) {
    $nodes= $xml_document->get_elements_b y_tagname($node _name);
    $nodeCount = count($nodes);
    $node = $nodes[0];
    if ($node) {
    $node = $node->first_child( );
    return $node->node_value() ;
    }
    }
    }


    The error is returned at the line 'return $node->node_value() ' but
    only when I use an empty node. Can this script be modified to allow
    empty nodes? Cheers

    Burnsy
  • Janwillem Borleffs

    #2
    Re: Can $node->node_value( ) return a null value?

    mr_burns wrote:[color=blue]
    > The error is returned at the line 'return $node->node_value() ' but
    > only when I use an empty node. Can this script be modified to allow
    > empty nodes? Cheers
    >[/color]

    Which error is returned and what is the structure of contacts.xml?


    JW



    Comment

    • mr_burns

      #3
      Re: Can $node->node_value( ) return a null value?

      "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message news:<413f04ce$ 0$46376$cd19a36 3@news.euronet. nl>...[color=blue]
      > mr_burns wrote:[color=green]
      > > The error is returned at the line 'return $node->node_value() ' but
      > > only when I use an empty node. Can this script be modified to allow
      > > empty nodes? Cheers
      > >[/color]
      >
      > Which error is returned?[/color]


      Fatal error: Call to a member function on a non-object in
      /home/martyn69/public_html/oarsome/includes/functions.php on line 38

      [color=blue]
      > and what is the structure of contacts.xml?[/color]


      <?xml version="1.0" encoding="iso-8859-1"?>
      <contacts>
      <address1>123 </address1>
      <address2>Fak e Street</address2>
      <address3>Sprin gfield</address3>
      <phone>0141 952 3161</phone>
      <mobile></mobile>
      <email>bissatch @yahoo.co.uk</email>
      </contacts>


      The error only seems to occur when I leave a node empty (see mobile
      node above).

      Cheers

      Martyn

      Comment

      • Janwillem Borleffs

        #4
        Re: Can $node-&gt;node_value( ) return a null value?

        mr_burns wrote:[color=blue]
        > The error only seems to occur when I leave a node empty (see mobile
        > node above).
        >[/color]

        This should do it:

        function foutput_contact ($node_name) {
        $xml_document = domxml_open_fil e('contacts.xml ');
        if ($xml_document) {
        $nodes= $xml_document->get_elements_b y_tagname($node _name);
        $nodeCount = count($nodes);
        $node = $nodes[0];

        if ($node && $node->has_child_node s()) {
        $node = $node->first_child( );
        return $node->node_value() ;
        } else {
        return "";
        }
        }
        }

        Or even simpler:

        function foutput_contact ($node_name) {
        $xml_document = domxml_open_fil e('contacts.xml ');
        if ($xml_document) {
        $nodes= $xml_document->get_elements_b y_tagname($node _name);
        $nodeCount = count($nodes);
        $node = $nodes[0];
        return $node ? $node->get_content( ) : "";
        }
        }


        JW



        Comment

        Working...