在php

时间:2016-06-08 08:08:44

标签: php xml oop parsing zend-framework

$data = 
    '<parent>
       <child>
         <EC>1</EC>
         <Descript>Savings Accepted</Descript>
         <Logic>RF > LF = PAID</Logic>
         <Rules>RF is greater than PAID and LF is equal to PAID</Rules>
       </child>
       <child>
         <EC>2</EC>
         <Descript>Savings Declined</Descript>
         <Logic>RF >= LF < PAID</Logic>
         <Rules>RF greater/equal PAID, LF equal PAID</Rules>
       </child>
     </parent>';

file_put_contents('rules.xml', $data);

$xml = simplexml_load_file("rules.xml")  or die("Error: Cannot create object");

我无法转换。

1 个答案:

答案 0 :(得分:0)

你应该使用实体&amp; lt;而不是&lt;和&amp; gt;而不是&gt;

$data = <<<'XML'
<parent><child>
    <EC>1</EC>
    <Descript>Savings Accepted</Descript>
    <Logic>RF &gt; LF = PAID</Logic>
    <Rules>RF is greater than PAID and LF is equal to PAID</Rules>
</child>
<child>
    <EC>2</EC>
    <Descript>Savings Declined</Descript>
    <Logic>RF &gt;= LF &lt; PAID</Logic>
    <Rules>RF greater/equal PAID, LF equal PAID</Rules>
</child></parent>
XML;
file_put_contents('rules.xml', $data);
$xml = simplexml_load_file("rules.xml") or die("Error: Cannot create object");

print_r($xml);

输出继电器

SimpleXMLElement Object
(
    [child] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [EC] => 1
                    [Descript] => Savings Accepted
                    [Logic] => RF > LF = PAID
                    [Rules] => RF is greater than PAID and LF is equal to PAID
                )

            [1] => SimpleXMLElement Object
                (
                    [EC] => 2
                    [Descript] => Savings Declined
                    [Logic] => RF >= LF  RF greater/equal PAID, LF equal PAID
                )

        )

)

来源:http://www.tutorialspoint.com/xml/xml_character_entities.htm https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references