GetElementById PHP5 DOM Problem

Discussion in 'XML' started by Skeets, Feb 22, 2006.

  1. Skeets

    Skeets Guest

    When the data structure looks like this:

    <page>
    <point id="1">
    <premise> 1+1=2 </premise>
    </point>
    <point id="2">
    <premise> round is better than square </premise>
    </point>
    <thought id="1">
    <note> I like Fridays </note>
    </thought>
    <thought id="2">
    <note> rsaturday's rock </note>
    </thought>
    </page>

    how do i use GetElementById() to get thought id 2's note?

    i saw some examples where the id was unique within the xml. in my
    case, though, the id isn't unique and i couldn't google up a single
    example. te syntax isn't intuitive to me so i haven't been able to
    work it out on my own, either.

    tia...
    tia...
     
    Skeets, Feb 22, 2006
    #1
    1. Advertising

  2. Skeets wrote:

    > When the data structure looks like this:
    >
    > <page>
    > <point id="1">
    > <premise> 1+1=2 </premise>
    > </point>
    > <point id="2">
    > <premise> round is better than square </premise>
    > </point>
    > <thought id="1">
    > <note> I like Fridays </note>
    > </thought>
    > <thought id="2">
    > <note> rsaturday's rock </note>
    > </thought>
    > </page>
    >
    > how do i use GetElementById() to get thought id 2's note?


    getElementById on XML is only going to work if there is a DTD that
    defines the attributes of type ID. And ID attribute values are required
    to be unique in the document. And ID attribute values have to begin with
    a letter so your values starting with/consisting of a digit are not
    valid ID values.

    I guess you could use XPath to look for point elements with @id being 2 e.g.
    /page/thought[@id = '2']/note/text()
    with PHP 5

    $xPathEvaluator = new DOMXPath($xmlDocument);
    $nodeList = $xPathEvaluator->query(
    "/page/thought[@id = '2']/note/text()", $xmlDocument);
    foreach ($nodeList as $node) {
    echo $node->data . "<br>\r\n";
    }



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 22, 2006
    #2
    1. Advertising

  3. Skeets

    Skeets Guest

    Thanks Martin.

    i'm receiving an error when tryiong to use xpath.

    i used the example here:

    http://us2.php.net/manual/en/function.xpath-eval-expression.php

    i cipied and pasted it and it get the following error:

    PHP Fatal error: Call to undefined function domxml_open_mem() in
    C:\web\html\dmt\_debug_tmp.php on line 497
     
    Skeets, Feb 23, 2006
    #3
  4. Skeets wrote:


    > i'm receiving an error when tryiong to use xpath.


    I think you said you use PHP 5. My earlier post contains an example that
    works with PHP 5.

    > i used the example here:
    >
    > http://us2.php.net/manual/en/function.xpath-eval-expression.php
    >
    > i cipied and pasted it and it get the following error:
    >
    > PHP Fatal error: Call to undefined function domxml_open_mem() in
    > C:\web\html\dmt\_debug_tmp.php on line 497


    That stuff is part of a PHP 4 only extension for DOM and XPath. You
    can't use that with PHP 5 and you really don't want to.
    If you are using PHP 4 now and want to use that code then you need to
    make sure that DOM XML extension for PHP 4 is installed.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 23, 2006
    #4
  5. Skeets

    Skeets Guest

    Martin, for some reason i was having lots of trouble.

    i eventually found out how to do everything i needed, and more, in
    PHP5's simplexml.

    this tutorial is excellent:

    http://www.isolated-designs.net/core/article/25/simplexml-and-xpath-in-php-5

    the author switched from

    simplexml_load_file('applications.xml');

    to

    $apps = simplexml_load_string($xml);

    midstream - with no comment. for the newbs, you only need one or the
    other - depending on if you are importing a file or working with a
    string (included from another file or actually in the same file).

    back to my problem, the solution is as simple as:

    <?php
    $page = simplexml_load_string($xml_string);
    $thought = $page->xpath("//thought[@id = 2]");
    echo $thought[0]->note;
    ?>

    this should print the note, although i don't have time to test this
    code. i did test the code in the tutorial and it worked liked a charm.

    thanks for helping - i'm not sure what i was doing to gaff things, but
    this method is the simplest around. being a newb, i like that a lot.
     
    Skeets, Feb 23, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Willy Kreim
    Replies:
    15
    Views:
    685
    Dale King
    Apr 15, 2006
  2. Jeff

    php5 or asp.net

    Jeff, Jun 26, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    556
    =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=
    Sep 7, 2006
  3. Replies:
    2
    Views:
    17,452
    IchBin
    Aug 15, 2006
  4. romiro

    PHP5 programmer learning Python

    romiro, May 27, 2007, in forum: Python
    Replies:
    5
    Views:
    307
    =?iso-8859-1?q?Luis_M=2E_Gonz=E1lez?=
    May 28, 2007
  5. Patrick

    a question on xslt with php5

    Patrick, Jun 26, 2007, in forum: XML
    Replies:
    2
    Views:
    377
    Patrick
    Jun 26, 2007
Loading...

Share This Page