simplexml_load_file不适用于某些xml

时间:2017-12-06 13:19:06

标签: php xml-parsing simplexml simplexml-load-string

我有一个基本的公共行来解析文件中的xml。 问题是 XML 2 解析正常,但 XML 1 返回NULL。 我检查了所有错误,查看了所有内容,但还没有弄清楚。

还尝试使用file_contents但仍然无效。

$xml =(simplexml_load_file($data, 'SimpleXMLElement', LIBXML_NOCDATA));

XML 1

<?xml version="1.0" encoding="utf-8"?>
<msg:Message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="urn:xmlns:SSE-HomeServices:Messages:Inbound">
  <msg:MessageHeader>
    <msg:senderOrgId>Actavo</msg:senderOrgId>
    <msg:senderTimeStamp>2017-12-06T09:16:57</msg:senderTimeStamp>
    <msg:contractId>CNR-420781-Z7L8Q8</msg:contractId>
  </msg:MessageHeader>
  <msg:Response>
    <msg:WorkOrders>
      <msg:WorkOrder>
        <msg:woNumber>W00521062</msg:woNumber>
        <msg:woNotes>Leak and aav all sorted .
Boiler now operating correctly and no leaks 
Sop7 done No combustion affected Visuals done </msg:woNotes>            
        <msg:isProductSwitchRequired>No</msg:isProductSwitchRequired>
        <msg:productSwitchReason xsi:nil="true" />            
      </msg:WorkOrder>
    </msg:WorkOrders>
  </msg:Response>
</msg:Message>

XML 2

<Message xmlns="urn:xmlns:SSE-HomeServices:Messages:Outbound" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <MessageHeader>
    <senderOrgId>SSE</senderOrgId>
    <senderTimeStamp>2017-10-16T15:50:53.2291513+01:00</senderTimeStamp>
  </MessageHeader>
  <Request>
    <WorkOrders>
      <WorkOrder>
        <woNumber>W00617195</woNumber>
        <woCategory>Maintenance</woCategory>
        <woGroup>Gas Work</woGroup>
        <woType>Same Day Breakdown + Service</woType>
        <woInstruction>Cust has no CH or HW. Can an initial inspection be carried out too please. Please can we phone this customer asap due to previous lack of communication. Promotion :N/A Base Product Name :Boiler Cover Addon Product Name :Gas Service Feature Product
          Name :N/A Staff :False Landlord :False</woInstruction>
        <isCentralHeatingOperational>No</isCentralHeatingOperational>
        <isHotWaterOperational>No</isHotWaterOperational>
        <isCallOutChargeable>Non Chargeable</isCallOutChargeable>
        <isPartsChargeable>Determine on site</isPartsChargeable>
        <isLabourChargeable>Determine on site</isLabourChargeable>
        <dueDate>2017-10-17T14:49:00Z</dueDate>
        <name>Same Day Breakdown + Service workorder for Daniel Ward</name>
        <appointmentNumber>1</appointmentNumber>                    
      </WorkOrder>
    </WorkOrders>
  </Request>
</Message>

1 个答案:

答案 0 :(得分:0)

我认为对于xml 1,您应该将msg的名称空间添加到$xml =simplexml_load_file($data, 'SimpleXMLElement', LIBXML_NOCDATA, "urn:xmlns:SSE-HomeServices:Messages:Inbound"); ,如:

constexpr

您还可以从代码中删除一组括号。