Parsing - Reading data within a tag

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

    Parsing - Reading data within a tag

    Hi,

    I am new to XML and PHP and have a question that I hope someone could
    answer.

    Some background on my problem.
    I am receiving an XML message over TCP/IP and need to access data
    within the tags of the message.

    Message example:
    <message title="begin"
    no="1"><descrip tion>Start</description></message>


    Now I know I can get the data from the description tag by:

    $xmlstr =<<<EOT
    <message title="begin"
    no="1"><descrip tion>Start</description></message>
    EOT;

    $xml = simplexml_load_ string($xmlstr) ;
    echo ($xml->description[0]);


    My question is, how do I access 'title' or 'no' ?
    Is it possible?

    I cannot change the format of the data sent to me.
    I am using PHP 5.0.5

    Any help is appreciated.

    Also, is this the best way of parsing the XML message?

    Thank you

    Steve

  • Oli Filth

    #2
    Re: Parsing - Reading data within a tag

    Steve said the following on 11/10/2005 21:35:[color=blue]
    > Some background on my problem.
    > I am receiving an XML message over TCP/IP and need to access data
    > within the tags of the message.
    >
    > Message example:
    > <message title="begin"
    > no="1"><descrip tion>Start</description></message>
    >
    >
    > Now I know I can get the data from the description tag by:
    >
    > $xml = simplexml_load_ string($xmlstr) ;
    > echo ($xml->description[0]);
    >
    >
    > My question is, how do I access 'title' or 'no' ?
    > Is it possible?
    >[/color]


    [color=blue]
    > Also, is this the best way of parsing the XML message?
    >[/color]

    The SimpleXML functions convert an XML document into a nested data
    structure in one operation, but this could require a fair chunk of
    memory, depending on the size and complexity of the XML data.

    The basic XML functions, however, provide an event-based parsing scheme.
    i.e. the parser reads from top to bottom, and every time an element is
    opened or closed, a user-defined function is called. This allows you to
    perform necessary processing without chewing up large amounts of memory.

    See http://www.php.net/manual/ref.xml.php



    --
    Oli

    Comment

    Working...