我已经在这个问题上待了一个星期左右,并且没有在已经存在的导致解决方案的问题中找到任何信息。我从一家公司(它是一个MITS Feed)中提取了一个非常大的XML文件,它可以轻松超过一百万行输出。遗憾的是,由于法律原因,我无法提供此文件的位置,但我会解释我正在尝试做什么,也许你们都可以帮我解决这个问题。
我正在调用文件:
<?php
$xml=simplexml_load_file("location-of-file/feed.xml");
?>
在此之后,我想要浏览并提取每个属性的信息。这是我们使用的确切模式 -
http://www.mitsproject.com/Content/ServeFile.cfm?FileID=4075
我尝试将其作为DOMDocument导入并以这种方式遍历XML,但没有运气。可能只是一个如何在PHP for循环中拉出所有Property节点的例子就是炸弹。提前谢谢。
P.S。我知道文件至少被抓住了,因为当我跑 -
<?php
$xml = simplexml_load_file("location-of-file/feed.xml", null, LIBXML_NOCDATA);
print_r($xml);
?>
我得到了一个疯狂的大输出。
更新:
以下是一个'Property'作为SimpleXMLElement的示例,也许这会有所帮助 -
[111] => SimpleXMLElement Object
( [@attributes] =&gt;排列 ( [IDValue] =&gt; 56bce392-b9fb-4290-97bc-900b9ebf9a1e )
[PropertyID] => SimpleXMLElement Object
(
[Identification] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
[OrganizationName] => rentershq
[IDType] => property
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[IDValue] => 1a47b013fa94796a8973a23cc8b01192ba603460
[OrganizationName] => rentershq
[IDType] => Company
)
)
)
[MarketingName] => Clean Older Unit
[WebSite] => http://rentershq.appfolio.com/listings/listings/56bce392-b9fb-4290-97bc-900b9ebf9a1e
[Address] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AddressType] => property
)
[Description] => Address of Available Listing
[AddressLine1] => 2850 PIONEER DRIVE - 17
[City] => REDDING
[State] => CA
[PostalCode] => 96001
[Country] => US
)
[Phone] => SimpleXMLElement Object
(
[@attributes] => Array
(
[PhoneType] => office
)
[PhoneNumber] => (530) 722-0800
)
[Email] => james@rentershq.com
)
[ILS_Identification] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ILS_IdentificationType] => Apartment
[RentalType] => Market Rate
)
[Latitude] => 40.5873599
[Longitude] => -122.41376
[LastUpdate] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Month] => 12
[Day] => 4
[Year] => 2013
)
)
)
[Information] => SimpleXMLElement Object
(
[StructureType] => Standard
[UnitCount] => 1
[ShortDescription] => Clean Older Unit
[LongDescription] => Please drive by property. Please contact onsite manager Joe Skeen to view inside of unit. 530-255-8375
[Rents] => SimpleXMLElement Object
(
[StandardRent] => 500.00
)
[PropertyAvailabilityURL] => http://rentershq.appfolio.com/listings/listings/56bce392-b9fb-4290-97bc-900b9ebf9a1e
)
[Fee] => SimpleXMLElement Object
(
[ProrateType] => Standard
[LateType] => Standard
[LatePercent] => 0
[LateMinFee] => 0
[LateFeePerDay] => 0
[NonRefundableHoldFee] => 0
[AdminFee] => 0
[ApplicationFee] => 15.00
[BrokerFee] => 0
)
[Deposit] => SimpleXMLElement Object
(
[@attributes] => Array
(
[DepositType] => Security Deposit
)
[Amount] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmountType] => Actual
)
[ValueRange] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Exact] => 700.00
[Currency] => USD
)
)
)
)
[Policy] => SimpleXMLElement Object
(
[Pet] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Allowed] => false
)
)
)
[Phase] => SimpleXMLElement Object
(
[@attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
)
[Name] => SimpleXMLElement Object
(
)
[Description] => SimpleXMLElement Object
(
)
[UnitCount] => 1
[RentableUnits] => 1
[TotalSquareFeet] => 625
[RentableSquareFeet] => 625
)
[Building] => SimpleXMLElement Object
(
[@attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
)
[Name] => SimpleXMLElement Object
(
)
[Description] => SimpleXMLElement Object
(
)
[UnitCount] => 1
[SquareFeet] => 625
)
[Floorplan] => SimpleXMLElement Object
(
[@attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
)
[Name] => SimpleXMLElement Object
(
)
[UnitCount] => 1
[Room] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[RoomType] => Bedroom
)
[Count] => 2
[Comment] => SimpleXMLElement Object
(
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[RoomType] => Bathroom
)
[Count] => 1
[Comment] => SimpleXMLElement Object
(
)
)
)
[SquareFeet] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Min] => 625
[Max] => 625
)
)
[MarketRent] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Min] => 500
[Max] => 500
)
)
[EffectiveRent] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Min] => 500
[Max] => 500
)
)
)
[ILS_Unit] => SimpleXMLElement Object
(
[@attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
)
[Units] => SimpleXMLElement Object
(
[Unit] => SimpleXMLElement Object
(
[Identification] => SimpleXMLElement Object
(
[@attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
[OrganizationName] => RentersHQ
)
)
[MarketingName] => Clean Older Unit
[UnitBedrooms] => 2
[UnitBathrooms] => 1.0
[MinSquareFeet] => 625
[MaxSquareFeet] => 625
[SquareFootType] => internal
[UnitRent] => 500.00
[MarketRent] => 500.00
[Address] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AddressType] => property
)
[AddressLine1] => 2850 PIONEER DRIVE - 17
[City] => REDDING
[PostalCode] => 96001
[Country] => US
)
)
)
[Availability] => SimpleXMLElement Object
(
[VacateDate] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Month] => 7
[Day] => 26
[Year] => 2013
)
)
[VacancyClass] => Unoccupied
[MadeReadyDate] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Month] => 7
[Day] => 26
[Year] => 2013
)
)
)
[Amenity] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Other
)
[Description] => Ground Level Apartment
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Other
)
[Description] => HUD Considered
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Other
)
[Description] => Month-to-Month Lease
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Other
)
[Description] => Credit Check Required
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Other
)
[Description] => Stove
)
[5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Other
)
[Description] => Window Coverings
)
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Carpet
)
[Description] => Tille and Carpet
)
[7] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Other
)
[Description] => Off Street Parking
)
[8] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Dryer
)
[Description] => Coin-Op Washer / Dryer
)
[9] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Washer
)
[Description] => Coin-Op Washer / Dryer
)
[10] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Refrigerator
)
[Description] => Refrigerator
)
[11] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => DishWasher
)
[Description] => Dishwasher
)
[12] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Washer
)
[Description] => Dishwasher
)
[13] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Disposal
)
[Description] => Garbage Disposal
)
[14] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Heat
)
[Description] => Elec. Heating
)
[15] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Other
)
[Description] => Window Cooler
)
[16] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Other
)
[Description] => Dog Not Allowed
)
[17] => SimpleXMLElement Object
(
[@attributes] => Array
(
[AmenityType] => Other
)
[Description] => Cat Not Allowed
)
)
)
[File] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Active] => true
[FileID] => 809492874
)
[FileType] => Photo
[Description] => Unit Photo
[Name] => SimpleXMLElement Object
(
)
[Caption] => SimpleXMLElement Object
(
)
[Format] => image/jpeg
[Src] => http://pa.cdn.appfolio.com/rentershq/images/8ed6dbf4-36cf-47e4-b519-7783bea727dc/medium.jpg
[Width] => 1280
[Height] => 960
[Rank] => 1
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Active] => true
[FileID] => 809492885
)
[FileType] => Photo
[Description] => Unit Photo
[Name] => SimpleXMLElement Object
(
)
[Caption] => SimpleXMLElement Object
(
)
[Format] => image/jpeg
[Src] => http://pa.cdn.appfolio.com/rentershq/images/9b6b35e7-0ca1-4c20-a396-4be6fb21bf80/medium.jpg
[Width] => 1280
[Height] => 960
[Rank] => 2
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Active] => true
[FileID] => 809492887
)
[FileType] => Photo
[Description] => Unit Photo
[Name] => SimpleXMLElement Object
(
)
[Caption] => SimpleXMLElement Object
(
)
[Format] => image/jpeg
[Src] => http://pa.cdn.appfolio.com/rentershq/images/c6ab69e3-0787-4221-893d-fbd982c2a3bd/medium.jpg
[Width] => 1280
[Height] => 960
[Rank] => 3
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Active] => true
[FileID] => 809492889
)
[FileType] => Photo
[Description] => Unit Photo
[Name] => SimpleXMLElement Object
(
)
[Caption] => SimpleXMLElement Object
(
)
[Format] => image/jpeg
[Src] => http://pa.cdn.appfolio.com/rentershq/images/6ac14176-73c3-452b-80ae-b41820887c73/medium.jpg
[Width] => 1280
[Height] => 960
[Rank] => 4
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Active] => true
[FileID] => 809492892
)
[FileType] => Photo
[Description] => Unit Photo
[Name] => SimpleXMLElement Object
(
)
[Caption] => SimpleXMLElement Object
(
)
[Format] => image/jpeg
[Src] => http://pa.cdn.appfolio.com/rentershq/images/252a2ae1-66ea-471d-8cf1-48e795add81d/medium.jpg
[Width] => 960
[Height] => 1280
[Rank] => 5
)
[5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Active] => true
[FileID] => 809492895
)
[FileType] => Photo
[Description] => Unit Photo
[Name] => SimpleXMLElement Object
(
)
[Caption] => SimpleXMLElement Object
(
)
[Format] => image/jpeg
[Src] => http://pa.cdn.appfolio.com/rentershq/images/b93c93f6-b571-4a33-a4e3-e0ac663679bc/medium.jpg
[Width] => 1280
[Height] => 960
[Rank] => 6
)
)
)
答案 0 :(得分:1)
尝试使用
输出Feedecho '<pre>';
print_r($feed);
echo '</pre>';
这应该给你一个更“漂亮”的方案视图以及哪些节点不可访问!
答案 1 :(得分:0)
我建议使用类似的东西:
$xml = simplexml_load_file("location-of-file/feed.xml", null, LIBXML_NOCDATA);
foreach($xml as $key => $item){
// to select an element do something like this
$item->WebSite;
}
但请注意$ item是数组$ xml的副本,因此要操作此对象,您需要将$ item更改为&amp; $ item,否则您需要在循环中操纵实际值
$test = $item->WebSite;
//do something to $test
$xml->$key->WebSite = $test;
如果您正在构建新的数组/ xml文档,则无需担心此问题
更新
我已经在示例Feed中对此进行了测试,您应该可以通过一些编辑来实现这一点
header("Content-Type: application/xml; charset=utf-8");
$xml = simplexml_load_file("test.xml", null, LIBXML_NOCDATA);
$values = array("Belgian Waffles","French Toast");
$new = new SimpleXMLElement("<tester></tester>");
foreach($xml->food as $item){
if(in_array($item->name,$values)){
$type = $new->addChild('type');
$movie = $type->addChild('movie');
$movie->addChild('price', $item->price);
}else{
}
}
echo $new->asXML();
基本上给这个以前,编辑它以使用正确的节点并为你构建适当的权限然后这应该工作。 asXML()有一个可选参数,你可以传入它来写文件
因此,将echo $new->asXML();
更改为$new->asXML("test.xml");
会将文件写入test.xml而不是输出数据。如果你保存它,请确保你摆脱header("Content-Type: application/xml; charset=utf-8");