PHP4 XML DOM

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

    PHP4 XML DOM

    Currently my web server supports only php4.
    I want to know how to parse the xml and get the
    element value using dom. I thought they have similar
    function for getting element by id or tag name, so
    far i haven't find anyting like that except fro PHP5 with
    Domdocument. Any help is appreciated.


    $dom = domxml_open_fil e ('data.xml');
    $e_data = $dom->document_eleme nt();

    <?xml version="1.0"?>
    <book>
    <title>hhelo</title>
    </book>



  • p.lepin@ctncorp.com

    #2
    Re: PHP4 XML DOM


    mich dobelman wrote:
    Currently my web server supports only php4.
    I want to know how to parse the xml and get the
    element value using dom. I thought they have similar
    function for getting element by id or tag name, so
    far i haven't find anyting like that except fro PHP5 with
    Domdocument. Any help is appreciated.
    I heartily recommend upgrading to PHP5 and DOM extension,
    it's much better than PHP4's DOM XML. Nevertheless, I hope
    this helps:

    $xml = '<?xml version="1.0"?>
    <book>
    <title>hhelo</title>
    </book>' ;

    // here we traverse shallowly all the children nodes of a
    // given node and add node values of all the text nodes
    // found to the result
    function get_value ( $node )
    {
    $result = '' ;
    $sub_nodes = $node -child_nodes ( ) ;
    foreach ( $sub_nodes as $sub_node )
    {
    if ( $sub_node -node_type ( ) == XML_TEXT_NODE )
    {
    $result .= $sub_node -node_value ( ) ;
    }
    }
    return ( $result ) ;
    }

    $doc = domxml_open_mem ( $xml ) ;

    // method #1
    // here we get all the <titleelement s in the document,
    // then get the contents of the first one of them
    $titles = $doc -get_elements_by _tagname ( 'title' ) ;
    $result_1 = get_value ( $titles [ 0 ] ) ;

    // method #2
    // here we get the first element node child of the root
    // element, then get its contents
    $books = $doc -child_nodes ( ) ;
    $book_ix = $title_ix = 0 ;
    while ( $books [ $book_ix ++ ] -node_type ( ) !=
    XML_ELEMENT_NOD E ) ;
    $titles = $books [ -- $book_ix ] -child_nodes ( ) ;
    while ( $titles [ $title_ix ++ ] -node_type ( ) !=
    XML_ELEMENT_NOD E ) ;
    $result_2 = get_value ( $titles [ -- $title_ix ] ) ;

    // method #3
    // the same thing, only using XPath
    $xpath = $doc -xpath_new_conte xt ( ) ;
    $result =
    $xpath -xpath_eval ( '//book/title/text()' ) ;
    $result_3 = $result -nodeset [ 0 ] -content ;

    --
    Pavel Lepin

    Comment

    • Shuan

      #3
      Re: PHP4 XML DOM

      Thanks, Pavel
      >$doc = domxml_open_mem ( $xml ) ;
      The function you provided works perfectly for local file, but
      not the remote one. Do i have to use the different function remote
      file?

      about PHP5
      I want to, but My ISP said not ready to upgrade to PHP5.





      Comment

      • p.lepin@ctncorp.com

        #4
        Re: PHP4 XML DOM


        Shuan wrote:
        $doc = domxml_open_mem ( $xml ) ;
        >
        The function you provided works perfectly for local file,
        but not the remote one. Do i have to use the different
        function remote file?
        Actually, domxml_open_mem () parses XML from a string. For
        both local and remote files you should use
        domxml_open_fil e() - well, depending on your definition of
        'remote' and your ISP's PHP settings ('allow_url_fop en').
        The following should work:

        $doc = domxml_open_fil e
        ( 'http://example.org/dir/data.xml' ) ;

        --
        Pavel Lepin

        Comment

        Working...