PHP4(旧版服务器)XML => JSON转换,la simplexml_file_load()或simpleXMLelement

时间:2013-10-30 20:46:40

标签: php xml json

我正在使用旧版本的PHP(4.3.9)在服务器上开发,我正在尝试将XML字符串转换为JSON字符串。这在PHP5中很容易,但PHP4要困难得多。

我试过了:

Zend JSON.php

require_once 'Zend/Json.php';
echo Zend_Json::encode($sxml);

错误:
PHP Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}'

simplexml44

$impl = new IsterXmlSimpleXMLImpl;
$NDFDxmltemp = $impl->load_file($NDFDstring);
$NDFDxml = $NDFDxmltemp->asXML();

错误:
WARNING isterxmlexpatnonvalid->parse(): nothing to read

xml_parse

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "_start_element", "_end_element");
xml_set_character_data_handler($xml_parser, "_character_data");  
xml_parse($xml_parser, $NDFDstring);

错误:
PHP Warning: xml_parse(): Unable to call handler _character_data() in ...
PHP Warning: xml_parse(): Unable to call handler _end_element() in ...

在PHP4中,有没有其他人可以选择simplexml_file_load()new simpleXMLelement

在这种特殊情况下,升级PHP不是一个选项,所以不要打扰它。是的,我知道它已经老了。

注意:这是我正在尝试解析为多维数组OR json的XML。 http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?lat=40&lon=-120&product=time-series&begin=2013-10-30T00:00:00&end=2013-11-06T00:00:00&maxt=maxt&mint=mint&rh=rh&wx=wx&wspd=wspd&wdir=wdir&icons=icons&wgust=wgust&pop12=pop12&maxrh=maxrh&minrh=minrh&qpf=qpf&snow=snow&temp=temp&wwa=wwa

2 个答案:

答案 0 :(得分:2)

XML很容易自己解析,无论如何这里存在{4}在php 4和xml_parse

这里是domxml_open_filejson for php4

根据解析xml:

如果你知道xml文件的结构,你甚至可以使用RegExp,因为XML是严格的格式(我的意思是所有标签都必须关闭,引号中的所有属性,所有特殊符号总是被转义)

如果您解析任意xml文件,这里是学生样本,它与php4一起使用,不了解所有XML功能,但可以给你“蛮力”的想法:

<?php
    define("LWG_XML_ELEMENT_NULL", "0");
    define("LWG_XML_ELEMENT_NODE", "1");

    function EntitiesToString($str)
    {
        $s = $str;

        $s = eregi_replace("&quot;", "\"", $s);
        $s = eregi_replace("&lt;", "<", $s);
        $s = eregi_replace("&gt;", ">", $s);
        $s = eregi_replace("&amp;", "&", $s);

        return $s;
    }

    class CLWG_dom_attribute
    {
        var $name;
        var $value;

        function CLWG_dom_attribute()
        {
            $name = "";
            $value = "";
        }
    }

    class CLWG_dom_node
    {
        var $m_Attributes;
        var $m_Childs;

        var $m_nAttributesCount;
        var $m_nChildsCount;

        var $type;
        var $tagname;
        var $content;

        function CLWG_dom_node()
        {
            $this->m_Attributes = array();
            $this->m_Childs = array();

            $this->m_nAttributesCount = 0;
            $this->m_nChildsCount = 0;

            $this->type = LWG_XML_ELEMENT_NULL;
            $this->tagname = "";
            $this->content = "";
        }

        function get_attribute($attr_name)
        {
            //echo "<message>Get Attribute: ".$attr_name." ";
            for ($i=0; $i<sizeof($this->m_Attributes); $i++)
                if ($this->m_Attributes[$i]->name == $attr_name)
                {
                    //echo $this->m_Attributes[$i]->value . "</message>\n";
                    return $this->m_Attributes[$i]->value;
                }
            //echo "[empty]</message>\n";
            return "";
        }
        function get_content()
        {
            //echo "<message>Get Content: ".$this->content . "</message>\n";
            return $this->content;
        }

        function attributes()
        {
            return $this->m_Attributes;
        }
        function child_nodes()
        {
            return $this->m_Childs;
        }

        function loadXML($str, &$i)
        {
            //echo "<debug>DEBUG: LoadXML (".$i.": ".$str[$i].")</debug>\n";
            $str_len = strlen($str);

            //echo "<debug>DEBUG: start searching for tag (".$i.": ".$str[$i].")</debug>\n";
            while ( ($i<$str_len) && ($str[$i] != "<") )
                $i++;
            if ($i == $str_len) return FALSE;
            $i++;

            while ( ($i<strlen($str)) && ($str[$i] != " ") && ($str[$i] != "/") && ($str[$i] != ">") )
                $this->tagname .= $str[$i++];

            //echo "<debug>DEBUG: Tag: " . $this->tagname . "</debug>\n";

            if ($i == $str_len) return FALSE;
            switch ($str[$i])
            {
                case " ": // attributes comming
                {
                    //echo "<debug>DEBUG: Tag: start searching attributes</debug>\n";
                    $i++;
                    $cnt = sizeof($this->m_Attributes);
                    while ( ($i<strlen($str)) && ($str[$i] != "/") && ($str[$i] != ">") )
                    {
                        $this->m_Attributes[$cnt] = new CLWG_dom_attribute;
                        while ( ($i<strlen($str)) && ($str[$i] != "=") )
                            $this->m_Attributes[$cnt]->name .= $str[$i++];
                        if ($i == $str_len) return FALSE;
                        $i++;
                        while ( ($i<strlen($str)) && ($str[$i] != "\"") )
                            $i++;
                        if ($i == $str_len) return FALSE;
                        $i++;
                        while ( ($i<strlen($str)) && ($str[$i] != "\"") )
                            $this->m_Attributes[$cnt]->value .= $str[$i++];

                        $this->m_Attributes[$cnt]->value = EntitiesToString($this->m_Attributes[$cnt]->value);

                        //echo "<debug>DEBUG: Tag: Attribute: '".$this->m_Attributes[$cnt]->name."' = '".$this->m_Attributes[$cnt]->value."'</debug>\n";

                        if ($i == $str_len) return FALSE;
                        $i++;
                        if ($i == $str_len) return FALSE;
                        while ( ($i<strlen($str)) && ($str[$i] == " ") )
                            $i++;

                        $cnt++;
                    }
                    if ($i == $str_len) return FALSE;
                    switch ($str[$i])
                    {
                        case "/":
                        {
                            //echo "<debug>DEBUG: self closing tag with attributes (".$this->tagname.")</debug>\n";
                            $i++;
                            if ($i == $str_len) return FALSE;
                            if ($str[$i] != ">") return FALSE;
                            $i++;
                            return TRUE;
                            break;
                        }
                        case ">";
                        {
                            //echo "<debug>DEBUG: end of attributes (".$this->tagname.")</debug>\n";
                            $i++;
                            break;
                        }
                    }
                    break;
                }
                case "/": // self closing tag
                {
                    //echo "<debug>DEBUG: self closing tag (".$this->tagname.")</debug>\n";
                    $i++;
                    if ($i == $str_len) return FALSE;
                    if ($str[$i] != ">") return FALSE;
                    $i++;
                    return TRUE;
                    break;
                }
                case ">": // end of begin of node
                {
                    //echo "<debug>DEBUG: end of begin of node</debug>\n";
                    $i++;
                    break;
                }
            }
            if ($i == $str_len) return FALSE;

            $b = 1;

            while ( ($i<$str_len) && ($b) )
            {
                //echo "<debug>DEBUG: searching for content</debug>\n";
                while ( ($i<strlen($str)) && ($str[$i] != "<") )
                    $this->content .= $str[$i++];
                //echo "<debug>DEBUG: content: ".$this->content."</debug>\n";
                if ($i == $str_len) return FALSE;
                $i++;
                if ($i == $str_len) return FALSE;
                if ($str[$i] != "/") // new child
                {
                    $cnt = sizeof($this->m_Childs);
                    //echo "<debug>DEBUG: Create new child (" . $cnt . ")</debug>\n";

                    $this->m_Childs[$cnt] = new CLWG_dom_node;
                    $this->m_Childs[$cnt]->type = LWG_XML_ELEMENT_NODE;
                    $i--;
                    if ($this->m_Childs[$cnt]->loadXML($str, $i) === FALSE)
                        return FALSE;
                }
                else
                    $b = 0;
            }

            $i++;
            $close_tag = "";
            while ( ($i<strlen($str)) && ($str[$i] != ">") )
                $close_tag .= $str[$i++];
            //echo "<debug>DEBUG: close tag: ".$close_tag." - ".$this->tagname."</debug>\n";
            if ($i == $str_len) return FALSE;
            $i++;

            $this->content = EntitiesToString($this->content);
            //echo "<debug>DEBUG: content: ".$this->content."</debug>\n";

            return ($close_tag == $this->tagname);
        }
    }

    class CLWG_dom_xml
    {
        var $m_Root;

        function CLWG_dom_xml()
        {
            $this->m_Root = 0;
        }

        function document_element()
        {
            return $this->m_Root;
        }

        function loadXML($xml_string)
        {
            // check xml tag
            if (eregi("<\\?xml", $xml_string))
            {
                // check xml version
                $xml_version = array();
                if ( (eregi("<\\?xml version=\"([0-9\\.]+)\".*\\?>", $xml_string, $xml_version)) && ($xml_version[1] == 1.0) )
                {
                    // initialize root
                    $this->m_Root = new CLWG_dom_node;
                    $i = 0;
                    return $this->m_Root->loadXML(eregi_replace("<\\?xml.*\\?>", "", $xml_string), $i);
                }
                else
                {
                    echo "<error>Cannot find version attribute in xml tag</error>";
                    return FALSE;
                }
            }
            else
            {
                echo "<error>Cannot find xml tag</error>";
                return FALSE;
            }
        }
    }

    function lwg_domxml_open_mem($xml_string)
    {
        global $lwg_xml;
        $lwg_xml = new CLWG_dom_xml;

        if ($lwg_xml->loadXML($xml_string))
            return $lwg_xml;
        else
            return 0;
    }
?>

答案 1 :(得分:1)

PHP4不支持ArrayAccess,也没有所需的__get()__set()__toString()魔术方法,这些方法可以有效地阻止您创建模仿该功能的对象SimpleXMLElement

另外我要说自己创建一个由Simplexml完成​​的内存结构对PHP 4来说效果不好,因为它限制了OOP对象模型和垃圾收集。特别是我喜欢这里的 Flyweight 模式。

这让我想到你可能更想要一个事件或基于拉的XML解析器。

看看使用PHP解析XML的PHP​​ 4方法的XML Parser Functions。您可以在多年的PHP培训材料中找到很好的解释,并附带示例和其他内容。