Need help sorting a XML file

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

    Need help sorting a XML file

    I have a similar xml file and I redisplay it with the following array in
    PHP. What I would like to do is sort the <idfield before it is
    redisplayed. Looking for a sample code.

    Thanks

    <rss>
    <item>
    <id>3</id>
    <name>Brian</name>
    </item>
    <item>
    <id>1</id>
    <name>James</name>
    </item>
    <item>
    <id>5</id>
    <name>Mark</name>
    </item>
    <item>
    <id>2</id>
    <name>Keith</name>
    </item>
    <item>
    <id>4</id>
    <name>Derek</name>
    </item>
    </rss>

    <?php
    $url = "test.xml";
    $data = implode("", file($url));
    preg_match_all ("/<item>([^`]*?)<\/item>/", $data, $matches);

    // Begin feed
    header ("Content-Type: text/xml; charset=ISO-8859-1");
    echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
    ?>

    <rss>

    <?

    foreach ($matches[0] as $match) {

    preg_match ("/<id>([^`]*?)<\/id>/", $match, $temp);
    $id = $temp['1'];

    preg_match ("/<name>([^`]*?)<\/name>/", $match, $temp);
    $description = $temp['1'];

    // Echo RSS XML
    echo "<item>\n";
    echo "\t\t\t<id> " . $id . "</id>\n";
    echo "\t\t\t<nam e>" . $name . "</name>\n";
    echo "\t\t</item>\n";
    }

    ?></rss>


  • BJMurphy

    #2
    Re: Need help sorting a XML file

    On Aug 17, 11:12 am, "Brian" <br...@aol.comw rote:
    I have a similar xml file and I redisplay it with the following array in
    PHP. What I would like to do is sort the <idfield before it is
    redisplayed. Looking for a sample code.
    >
    Thanks
    >
    <rss>
    <item>
    <id>3</id>
    <name>Brian</name>
    </item>
    <item>
    <id>1</id>
    <name>James</name>
    </item>
    <item>
    <id>5</id>
    <name>Mark</name>
    </item>
    <item>
    <id>2</id>
    <name>Keith</name>
    </item>
    <item>
    <id>4</id>
    <name>Derek</name>
    </item>
    </rss>
    >
    <?php
    $url = "test.xml";
    $data = implode("", file($url));
    preg_match_all ("/<item>([^`]*?)<\/item>/", $data, $matches);
    >
    // Begin feed
    header ("Content-Type: text/xml; charset=ISO-8859-1");
    echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
    ?>
    >
    <rss>
    >
    <?
    >
    foreach ($matches[0] as $match) {
    >
    preg_match ("/<id>([^`]*?)<\/id>/", $match, $temp);
    $id = $temp['1'];
    >
    preg_match ("/<name>([^`]*?)<\/name>/", $match, $temp);
    $description = $temp['1'];
    >
    // Echo RSS XML
    echo "<item>\n";
    echo "\t\t\t<id> " . $id . "</id>\n";
    echo "\t\t\t<nam e>" . $name . "</name>\n";
    echo "\t\t</item>\n";
    >
    }
    >
    ?></rss>
    Well, I think you mean to use $description in that second to last echo
    statement, but that's not your question. If your ID's are unique, why
    not build an array that maps the ID to the data (in this case just a
    single string) and then you can sort and display as you please?

    Comment

    Working...