我正在尝试通过PHP函数转换xml数据,但我得到解析器错误

时间:2016-06-01 07:50:38

标签: php xml parsing error-handling xml-parsing

<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>10</EC>
        <Descript>Consolidator / Charter / Internet</Descript>
        <Logic>RF >= LF <= PAID</Logic>
        <Rules>RF greater/equal PAID, LF lower/equal PAID</Rules>
    </child>
    <child>
        <EC>11</EC>
        <Descript>Only Business or First class available</Descript>
        <Logic>RF = LF = PAID</Logic>
        <Rules>RF,LF and PAID equal</Rules>
    </child>
    <child>
        <EC>13</EC>
        <Descript>Advance Purchase request not met</Descript>
        <Logic>RF > LF < PAID</Logic>
        <Rules>RF greater PAID , LF lower PAID</Rules>
    </child>
    <child>
        <EC>15</EC>
        <Descript>Negotiated Corporate Rate</Descript>
        <Logic>RF >= LF <= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower/equal PAID</Rules>
    </child>
    <child>
        <EC>16</EC>
        <Descript>schedule requested</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>17</EC>
        <Descript>Agent Semi Touch</Descript>
        <Logic>RF => LF > PAID</Logic>
        <Rules>RF greater/equal LF,LF lower/equal PAID</Rules>
    </child>
    <child>
        <EC>18</EC>
        <Descript>inconvenient itinerary</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>19</EC>
        <Descript>inconvenient dates</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower 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>
    <child>
        <EC>20</EC>
        <Descript>inconvenient airline</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>21</EC>
        <Descript>Upgradable fare requested</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>22</EC>
        <Descript>Flights with connection accepted</Descript>
        <Logic>RF >= LF ><= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF any value</Rules>
    </child>
    <child>
        <EC>23</EC>
        <Descript>inconvenient aircraft/equipment</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>24</EC>
        <Descript>cannot get Frequent Flyer mileage</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>25</EC>
        <Descript>non-refundable ticket</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>26</EC>
        <Descript>penalty/restrictions</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>27</EC>
        <Descript>Savings Accepted-even if Business/First is allowed</Descript>
        <Logic>RF > LF = PAID</Logic>
        <Rules>RF greater PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>28</EC>
        <Descript>Refused Consolidator fare</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>3</EC>
        <Descript>Lowest fare Available</Descript>
        <Logic>RF = LF  = PAID</Logic>
        <Rules>RF,LF and PAID equal</Rules>
    </child>
    <child>
        <EC>30</EC>
        <Descript>Schedule requested</Descript>
        <Logic>RF ><= LF ><= PAID</Logic>
        <Rules>Online booking</Rules>
    </child>
    <child>
        <EC>31</EC>
        <Descript>too many connections</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>32</EC>
        <Descript>no seat selection included</Descript>
        <Logic>RF >= LF <= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower/equal PAID</Rules>
    </child>
    <child>
        <EC>33</EC>
        <Descript>No appropriate seating</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>34</EC>
        <Descript>Too many restrictions</Descript>
        <Logic>RF >LF < PAID</Logic>
        <Rules>RF greater PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>35</EC>
        <Descript>Travel to a supplier</Descript>
        <Logic>RF >= LF <= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower/equal PAID</Rules>
    </child>
    <child>
        <EC>36</EC>
        <Descript>Flight Pass Approved</Descript>
        <Logic>RF = LF = PAID=0.00</Logic>
        <Rules>RF,LF and PAID are zero</Rules>
    </child>
    <child>
        <EC>38</EC>
        <Descript>Upgrade for medical reasons</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>39</EC>
        <Descript>Transferred to Flight Pass</Descript>
        <Logic>RF >= LF > PAID</Logic>
        <Rules>PAID is 0,RF greater/equal LF,LF greater PAID</Rules>
    </child>
    <child>
        <EC>4</EC>
        <Descript>Business Class requested</Descript>
        <Logic>RF > LF < PAID</Logic>
        <Rules>RF greater PAID, LF lower PAID</Rules>
    </child>
    <child>
        <EC>40</EC>
        <Descript>Trip handled by a partner</Descript>
        <Logic>RF >= LF ><= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF any value</Rules>
    </child>
    <child>
        <EC>41</EC>
        <Descript>Business or First class authorized</Descript>
        <Logic>RF >= LF =< PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower/equal PAID</Rules>
    </child>
    <child>
        <EC>42</EC>
        <Descript>Maximum number of employees on the same flight</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>43</EC>
        <Descript>Same itinerary as another person is required</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>44</EC>
        <Descript>Allowed by policy</Descript>
        <Logic>RF >= LF <= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower/equal PAID</Rules>
    </child>
    <child>
        <EC>45</EC>
        <Descript>Class of service / restrictions</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>46</EC>
        <Descript>Carrier Preference</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>47</EC>
        <Descript>Stop / Connections / Flight Times</Descript>
        <Logic>RF >= LF < PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>5</EC>
        <Descript>First Class requested</Descript>
        <Logic>RF> LF <= PAID</Logic>
        <Rules>RF greater PAID, LF lower/equal PAID</Rules>
    </child>
    <child>
        <EC>50</EC>
        <Descript>Internal meeting or training</Descript>
        <Logic>RF >= LF ><= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF any value</Rules>
    </child>
    <child>
        <EC>51</EC>
        <Descript>External meeting or training</Descript>
        <Logic>RF >= LF ><= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF any value</Rules>
    </child>
    <child>
        <EC>52</EC>
        <Descript>Travel to a supplier</Descript>
        <Logic>RF >= LF ><= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF any value</Rules>
    </child>
    <child>
        <EC>53</EC>
        <Descript>Travel to a customer</Descript>
        <Logic>RF >= LF ><= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF any value</Rules>
    </child>
    <child>
        <EC>57</EC>
        <Descript>Spouse/Family</Descript>
        <Logic>RF >= LF ><= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF any value</Rules>
    </child>
    <child>
        <EC>59</EC>
        <Descript>direct flight requested</Descript>
        <Logic>RF> LF< PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>6</EC>
        <Descript>Upgrade</Descript>
        <Logic>RF >= LF <= PAID</Logic>
        <Rules>RF greater PAID, LF lower/equal PAID</Rules>
    </child>
    <child>
        <EC>7</EC>
        <Descript>Company Policy</Descript>
        <Logic>RF >= LF <= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower/equal PAID</Rules>
    </child>
    <child>
        <EC>76</EC>
        <Descript>refused alternate airport</Descript>
        <Logic>RF> LF< PAID</Logic>
        <Rules>RF greater/equal PAID ,LF lower PAID</Rules>
    </child>
    <child>
        <EC>8</EC>
        <Descript>Exchange / Seats / Extra</Descript>
        <Logic>RF = LF = PAID</Logic>
        <Rules>RF,LF and PAID equal</Rules>
    </child>
    <child>
        <EC>86</EC>
        <Descript>Rail</Descript>
        <Logic>RF >= LF ><= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF any value</Rules>
    </child>
    <child>
        <EC>9</EC>
        <Descript>Refund</Descript>
        <Logic>RF = LF = PAID</Logic>
        <Rules>RF,LF and PAID equal</Rules>
    </child>
    <child>
        <EC>94</EC>
        <Descript>Flight Pass - Purchase</Descript>
        <Logic>RF = PAID</Logic>
        <Rules>RF equal to PAID , no LF</Rules>
    </child>
    <child>
        <EC>95</EC>
        <Descript>Flight Pass - Exchange</Descript>
        <Logic>RF = LF = PAID</Logic>
        <Rules>RF,LF and PAID equal</Rules>
    </child>
    <child>
        <EC>96</EC>
        <Descript>Flight Pass- Cancellation</Descript>
        <Logic>RF = LF = PAID</Logic>
        <Rules>RF,LF and PAID equal</Rules>
        </child>
    <child>
        <EC>97</EC>
        <Descript>Online Booking (GetThere / ResX / Concur)</Descript>
        <Logic>RF >= LF ><= PAID</Logic>
        <Rules>RF greater/equal PAID ,LF any value</Rules>
    </child>
    <child>
        <EC>98</EC>
        <Descript>Flight Pass - Booking</Descript>
        <Logic>RF = LF = PAID</Logic>
        <Rules>RF,LF and PAID are zero</Rules>
    </child>
    <child>
        <EC>99</EC>
        <Descript>Ticket on Points (ex. Aeroplan)</Descript>
        <Logic>RF >= LF > = PAID</Logic>
        Rules>PAID is 0,RF greater/equal LF,LF greater PAID</Rules>
    </child>
</parent>

2 个答案:

答案 0 :(得分:0)

xml上有逻辑运算符,因此运行时会出现XML分析错误。尝试使用CDATA来避免这种情况,例如<Logic> <![CDATA[RF >= LF < PAID]]></Logic>并使用LIBXML_NOCDATA参数将CDATA合并为文本节点。

希望此代码可以帮助您

$string = <<<XML
<parent>
    <child>
        <EC>1</EC>
        <Descript>Savings Accepted</Descript>
        <Logic> <![CDATA[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> <![CDATA[RF >= LF < PAID]]></Logic>
        <Rules>RF greater/equal PAID, LF equal PAID</Rules>
    </child>
</parent>
XML;

$xml = simplexml_load_string($string,'SimpleXMLElement',LIBXML_NOCDATA);
$array = json_decode(json_encode($xml), true);
print_r($array); 

答案 1 :(得分:0)

$ arr = array(“&gt;”,“&lt;”,“RF&gt;”,“&lt; =”); $ rep = array(“&gt;”,“&lt;”,“RF&gt;”,“&lt; =”); $ xml = json_decode(json_encode((array)simplexml_load_string(str_replace($ arr,$ rep,$ data))),1); 的print_r($ XML);死;