XML_PARSE_INTO_STRUCT

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

    XML_PARSE_INTO_STRUCT

    Hi,
    I am trying to parse an xml file and I am having some problems. Here
    is a sample structure of my xml file.

    <title>Exampl e</title>
    <link rel="self" href="http://link0.com"/>

    <entry>
    <title>E1</title>
    <link href="http://link1.com"/>
    </entry>
    <entry>
    <title>E2</title>
    <link href="http://link2.com"/>
    </entry>

    I want to be able to output the following

    <p><a href="http://link0.com'">Exa mple</a></p>
    <p><a href="http://link1.com'">E1</a></p>
    <p><a href="http://link2.com'">E2</a></p>

    here is my code:
    $file="test.xml ";

    $xml_parser = xml_parser_crea te();

    $handle = fopen($file, "rb");
    while (!feof($handle) ) {
    $data .= fread($handle, 8192);
    }
    fclose($handle) ;

    xml_parse_into_ struct($xml_par ser, $data, $vals, $index);
    xml_parser_free ($xml_parser);

    foreach ($vals as $xml_elem) {
    if ($xml_elem['level'] == 1){
    // THIS IS WHERE I SOMEHOW GOING TO READ AHEAD OR ACCESS THE NEXT
    TAG=LINK AND SOMEHOW READ ITS ATTRIBUTE
    if ($xml_elem['tag'] == 'TITLE')
    printf ("<h1>%s</h1>\n", $xml_elem['value']);
    else if ($xml_elem['tag'] == 'SUBTITLE')
    printf ("<h3>%s</h3>\n", $xml_elem['value']);
    }

    The problem is write here because i don't know how I can first read
    the LINK attribute first and then the title.

    Thanks

  • frodo2000@gmail.com

    #2
    Re: XML_PARSE_INTO_ STRUCT

    On 17 Cze, 01:23, Jack <accpac...@hotm ail.comwrote:
    Hi,
    I am trying to parse an xml file and I am having some problems. Here
    is a sample structure of my xml file.
    >
    <title>Exampl e</title>
    <link rel="self" href="http://link0.com"/>
    >
    <entry>
    <title>E1</title>
    <link href="http://link1.com"/>
    </entry>
    <entry>
    <title>E2</title>
    <link href="http://link2.com"/>
    </entry>
    >
    I want to be able to output the following
    >
    <p><a href="http://link0.com'">Exa mple</a></p>
    <p><a href="http://link1.com'">E1</a></p>
    <p><a href="http://link2.com'">E2</a></p>
    >
    here is my code:
    $file="test.xml ";
    >
    $xml_parser = xml_parser_crea te();
    >
    $handle = fopen($file, "rb");
    while (!feof($handle) ) {
    $data .= fread($handle, 8192);}
    >
    fclose($handle) ;
    >
    xml_parse_into_ struct($xml_par ser, $data, $vals, $index);
    xml_parser_free ($xml_parser);
    >
    foreach ($vals as $xml_elem) {
    if ($xml_elem['level'] == 1){
    // THIS IS WHERE I SOMEHOW GOING TO READ AHEAD OR ACCESS THE NEXT
    TAG=LINK AND SOMEHOW READ ITS ATTRIBUTE
    if ($xml_elem['tag'] == 'TITLE')
    printf ("<h1>%s</h1>\n", $xml_elem['value']);
    else if ($xml_elem['tag'] == 'SUBTITLE')
    printf ("<h3>%s</h3>\n", $xml_elem['value']);
    }
    >
    The problem is write here because i don't know how I can first read
    the LINK attribute first and then the title.
    >
    Thanks
    I think that SimpleXML would be more convenient for your task. Below
    you have sample code:

    <?php
    $xml = "<test>".
    "<entry><title> Test1</title><link>www .test1.org</link></entry>".
    "<entry><title> Test2</title><link>www .test2.org</link></entry>".
    "</test>";

    $xmlTable = simplexml_load_ string($xml); //Use
    simplexml_load_ file($filename) for file loading

    for ($i = 0; $i < count($xmlTable->entry); $i++){
    echo '<p><a href="'.$xmlTab le->entry[$i]->link.'">'.$xml Table-
    >entry[$i]->title.'</a></p>';
    }
    ?>


    Comment

    Working...