Ticket #238 (closed Feature Request: wontfix)

Opened 12 months ago

Last modified 10 months ago

Add XML support via a library or helper

Reported by: brettalton Owned by: Shadowhand
Priority: major Milestone: 2.1
Component: Core Version: SVN HEAD
Keywords: XML Cc:

Description

As per http://ca3.php.net/xml.

XML takes a lot of markup to display an XML file:

<?php
$file = "data.xml";
$depth = array();

function startElement($parser, $name, $attrs)
{
    global $depth;
    for ($i = 0; $i < $depth[$parser]; $i++) {
        echo "  ";
    }
    echo "$name\n";
    $depth[$parser]++;
}

function endElement($parser, $name)
{
    global $depth;
    $depth[$parser]--;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
xml_parser_free($xml_parser);
?> 

Or convert XML to HTML:

<?php
$file = "data.xml";
$map_array = array(
    "BOLD"     => "B",
    "EMPHASIS" => "I",
    "LITERAL"  => "TT"
);

function startElement($parser, $name, $attrs)
{
    global $map_array;
    if (isset($map_array[$name])) {
        echo "<$map_array[$name]>";
    }
}

function endElement($parser, $name)
{
    global $map_array;
    if (isset($map_array[$name])) {
        echo "</$map_array[$name]>";
    }
}

function characterData($parser, $data)
{
    echo $data;
}

$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
xml_parser_free($xml_parser);
?> 

Lets add XML support as a helper for doing these sorts of functions (ie: xml::xml_to_html();)

Change History

Changed 12 months ago by brettalton

This has major implications on the feeds library because you will be able to not just parse(), but create() feeds as well.

feeds::parse(); feeds::create();

Changed 12 months ago by Shadowhand

  • owner set to Shadowhand
  • status changed from new to assigned

Changed 11 months ago by user

Classic is very complicated for read and parse, use:

http://php.net/manual/en/ref.simplexml.php

Using only objects

Changed 10 months ago by Shadowhand

  • status changed from assigned to closed
  • resolution set to wontfix

[SimpleXML] is a PHP5 extension that is enabled by default since PHP 5.0. It provides a very clean API and is easy to use.

Rather than creating a (slower) wrapper, I recommend using SimpleXML itself.

Note: See TracTickets for help on using tickets.