php & xml questions

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

    php & xml questions

    I would like to get the value of a tag in an xml file, the problem is
    that, his structure is not <item>value</item> but is:

    <Cube>
    - <Cube time="somevalue ">
    <Cube rate="the value i want to get" />
    <Cube rate="the value i want to get" />
    <Cube rate="the value i want to get" />
    and so on.....

    So, i have two questions, how do i get the values of the 3rd "<Cube>"
    when it is inside the tags and not ouside? and how do I choose the
    right one, having three of them?

    Thank you,

    Stefano

  • Malcolm Dew-Jones

    #2
    Re: php &amp; xml questions

    Stefano (texstefano@lib ero.it) wrote:
    : I would like to get the value of a tag in an xml file, the problem is
    : that, his structure is not <item>value</item> but is:

    : <Cube>
    : - <Cube time="somevalue ">
    : <Cube rate="the value i want to get" />
    : <Cube rate="the value i want to get" />
    : <Cube rate="the value i want to get" />
    : and so on.....

    : So, i have two questions, how do i get the values of the 3rd "<Cube>"
    : when it is inside the tags and not ouside? and how do I choose the
    : right one, having three of them?

    In php 4, I use the xml parser that comes with php. The xml parser is
    going to call a function that you have to write, and which you have to
    register before the parsing starts.

    e.g.
    $xml_parser = xml_parser_crea te();
    xml_set_element _handler( $xml_parser, "startEleme nt", "endElement ");

    You need to provide functions called startElement and endElement. The last
    question is how to get the third tag. To do that you need to count the
    tags as you find them. The simplist way is to use a global variable.

    So, your startElement function could look something like the following at
    the top


    $count=0;

    function startElement($p arser, $name, $attrs)
    {
    global $count;

    if ($name == 'Cube')
    {
    $count++;
    $the_rate = $attrs['rate'];

    if ($count==3)
    {
    die("all done, value = $the_rate\n");


    (Of course you probably wouldn't use die, but it illustrates the point.)




    --

    This space not for rent.

    Comment

    • Bzdziul

      #3
      Re: php &amp; xml questions

      U¿ytkownik "Malcolm Dew-Jones" <yf110@vtn1.vic toria.tc.ca> napisa³ w
      wiadomo¶ci news:42aa274b@n ews.victoria.tc .ca...[color=blue]
      > The simplist way is to use a global variable.[/color]

      /me thinks that better would be any static variable, but "de gustibus non
      disputantum est".

      To author of the topic: i'd use XPath expresion it's easy. how?

      eg.
      $dom=domxml_ope n_mem($xml);
      $xpath = xpath_new_conte xt(&$this->dom);
      $result=xpath_e val($xpath,'/cube/cube/cube/@rate');
      print $result[0]->value;

      Comment

      • Oli Filth

        #4
        Re: php &amp; xml questions

        Stefano said the following on 10/06/2005 21:40:[color=blue]
        > I would like to get the value of a tag in an xml file, the problem is
        > that, his structure is not <item>value</item> but is:
        >
        > <Cube>
        > - <Cube time="somevalue ">
        > <Cube rate="the value i want to get" />
        > <Cube rate="the value i want to get" />
        > <Cube rate="the value i want to get" />
        > and so on.....
        >
        > So, i have two questions, how do i get the values of the 3rd "<Cube>"
        > when it is inside the tags and not ouside? and how do I choose the
        > right one, having three of them?
        >[/color]

        See the PHP XML functions, at http://www.php.net/manual/en/ref.xml.php.
        Particularly xml_set_element _handler().


        --
        Oli

        Comment

        Working...