仅具有模式定义的解析XML文件

问题描述 投票:0回答:1

我有一个具有多个架构定义的xml uri。看起来像这样

    <edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
    <edmx:DataServices>
    <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="ODataService">
    <EntityType Name="OpenHouse">
    <Key>
    <PropertyRef Name="OpenHouseKey"/>
    </Key>
    <Property Name="AppointmentRequiredYN" Type="Edm.Boolean">
    <Annotation Term="Core.Description" String="Indicates whether or not the OpenHouse requires an appointment."/>
    </Property>
    <Property Name="BridgeModificationTimestamp" Type="Edm.DateTimeOffset" Precision="27">
    <Annotation Term="Core.Description" String="A timestamp representing when last this listing was modified"/>
    </Property>
    <Property Name="ListingId" Type="Edm.String" MaxLength="255">...</Property>
    <Property Name="ListingKey" Type="Edm.String" MaxLength="255">...</Property>
    <Property Name="ListingKeyNumeric" Type="Edm.Int64">...</Property>
    <Property Name="ModificationTimestamp" Type="Edm.DateTimeOffset" Precision="27">
    <Annotation Term="Core.Description" String="The transactional timestamp automatically recorded by the MLS system representing the date/time the Open House was last modified."/>
    </Property>
    <Property Name="OpenHouseAttendedBy" Type="OpenHouseEnums.OpenHouseAttendedBy">
    <Annotation Term="Core.Description" String="Will the open house be attended by a licensed agent? Options are attended by agent, attended by the seller or unattended."/>
    </Property>.
.
.
.
</Schema>
</edmx:DataServices>
</edmx:Edmx>

文件是来自API(带有auth令牌)的响应,因此我无法发布url,而且太大,无法在此处发布。

有多个模式定义http://prntscr.com/ppaynh

我想解析此uri,对于每个模式,获取所有EntityType及其属性和属性。我尝试使用与此类似的代码,但是我在处理XML方面的专业知识有限。

if (($response_xml_data = file_get_contents($url))===false){
    echo "Error fetching XML\n";
} else {
   libxml_use_internal_errors(true);
   $data = simplexml_load_string($response_xml_data);
   if (!$data) {
       echo "Error loading XML\n";
       foreach(libxml_get_errors() as $error) {
           echo "\t", $error->message;
       }
   } else {
      var_dump($data);
   }
} 

如何将这个XML放入数组中?谢谢

php xml xml-parsing
1个回答
1
投票
由于XML包含模式数据的默认名称空间,因此您将需要在每个步骤使用此名称空间来获取数据。首先要使用XPath查找<Schema>元素,则需要在文档中注册它。然后,稍后要获取结构中的项目,只需使用名称空间来获取子项。
© www.soinside.com 2019 - 2024. All rights reserved.