我有一个具有多个架构定义的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,并为每个Schema获取具有其属性和属性的所有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放入数组中? 谢谢
答案 0 :(得分:1)
由于XML包含模式数据的默认名称空间,因此您将需要在每个步骤中使用此名称空间来获取数据。首先要使用XPath查找<Schema>
元素,则需要在文档中注册它。然后,稍后要获取结构中的项目,只需使用名称空间来获取子项。
$schemaURI = "http://docs.oasis-open.org/odata/ns/edm";
$data->registerXPathNamespace("d", $schemaURI);
foreach ( $data->xpath("//d:Schema") as $schema) {
echo "Namespace=".$schema['Namespace'].PHP_EOL;
$schemData = $schema->children($schemaURI);
foreach ( $schemData->EntityType as $entityType ) {
echo "Entity name=".$entityType->attributes()['Name'].PHP_EOL;
$entityTypeData = $entityType->children($schemaURI);
foreach ( $entityTypeData->Property as $propertyType ) {
echo "Property name=".$propertyType->attributes()['Name'].PHP_EOL;
}
}
}
带有测试数据,这给出了...
Namespace=ODataService
Entity name=OpenHouse
Property name=AppointmentRequiredYN
Property name=BridgeModificationTimestamp
Property name=ListingId
Property name=ListingKey
Property name=ListingKeyNumeric
Property name=ModificationTimestamp
Property name=OpenHouseAttendedBy