Im trying to return a list of contacts from the Xero API however when getting the response back from there api i cannot run the array through a for each loop.
when trying to loop through nothing is displayed, has anyone had this problem in the past?
im trying to loop through to put each contact within a row inside a table.
if i run
pr($contacts->Contacts[0]);
it returns the list of contacts however it wont display anything with the for loop below
This is my for loop
foreach($contacts->Contacts[0] as $item) {
echo $item['Id'];
echo $item['filepath'];
}
this is the array returned
SimpleXMLElement Object
(
[Id] => 0dcda838-d354-4674-b6eb-d788e166b8dd
[Status] => OK
[ProviderName] => TestIntergration
[DateTimeUTC] => 2017-09-18T01:42:30.7361585Z
[Contacts] => SimpleXMLElement Object
(
[Contact] => Array
(
[0] => SimpleXMLElement Object
(
[ContactID] => 0ae2c0f4-cae9-4431-ab6b-52554f1c7c69
[ContactStatus] => ACTIVE
[Name] => Steve Buscemi
[Addresses] => SimpleXMLElement Object
(
[Address] => Array
(
[0] => SimpleXMLElement Object
(
[AddressType] => STREET
)
[1] => SimpleXMLElement Object
(
[AddressType] => POBOX
)
)
)
[Phones] => SimpleXMLElement Object
(
[Phone] => Array
(
[0] => SimpleXMLElement Object
(
[PhoneType] => DDI
)
[1] => SimpleXMLElement Object
(
[PhoneType] => DEFAULT
)
[2] => SimpleXMLElement Object
(
[PhoneType] => FAX
)
[3] => SimpleXMLElement Object
(
[PhoneType] => MOBILE
)
)
)
[UpdatedDateUTC] => 2017-08-30T08:14:03.88
[IsSupplier] => false
[IsCustomer] => true
[HasAttachments] => false
)
[1] => SimpleXMLElement Object
(
[ContactID] => ab8a9286-740e-4246-876c-9fcac558d600
[ContactStatus] => ACTIVE
[Name] => Martin Hudson
[Addresses] => SimpleXMLElement Object
(
[Address] => Array
(
[0] => SimpleXMLElement Object
(
[AddressType] => STREET
)
[1] => SimpleXMLElement Object
(
[AddressType] => POBOX
)
)
)
[Phones] => SimpleXMLElement Object
(
[Phone] => Array
(
[0] => SimpleXMLElement Object
(
[PhoneType] => DDI
)
[1] => SimpleXMLElement Object
(
[PhoneType] => DEFAULT
)
[2] => SimpleXMLElement Object
(
[PhoneType] => FAX
)
[3] => SimpleXMLElement Object
(
[PhoneType] => MOBILE
)
)
)
[UpdatedDateUTC] => 2017-08-30T08:20:03.263
[IsSupplier] => false
[IsCustomer] => true
[HasAttachments] => false
)
[2] => SimpleXMLElement Object
(
[ContactID] => 463e9f23-d1a1-4d74-b19e-041142c8fd34
[ContactStatus] => ACTIVE
[Name] => Orlena Greenville
[Addresses] => SimpleXMLElement Object
(
[Address] => Array
(
[0] => SimpleXMLElement Object
(
[AddressType] => STREET
)
[1] => SimpleXMLElement Object
(
[AddressType] => POBOX
)
)
)
[Phones] => SimpleXMLElement Object
(
[Phone] => Array
(
[0] => SimpleXMLElement Object
(
[PhoneType] => DDI
)
[1] => SimpleXMLElement Object
(
[PhoneType] => DEFAULT
)
[2] => SimpleXMLElement Object
(
[PhoneType] => FAX
)
[3] => SimpleXMLElement Object
(
[PhoneType] => MOBILE
)
)
)
[UpdatedDateUTC] => 2017-09-01T07:56:32.667
[IsSupplier] => false
[IsCustomer] => false
[HasAttachments] => false
)
[3] => SimpleXMLElement Object
(
[ContactID] => b75bfe5b-cc6e-45ee-b0a1-feafe866088d
[ContactStatus] => ACTIVE
[Name] => asdfasdfasdf454
[Addresses] => SimpleXMLElement Object
(
[Address] => Array
(
[0] => SimpleXMLElement Object
(
[AddressType] => STREET
)
[1] => SimpleXMLElement Object
(
[AddressType] => POBOX
)
)
)
[Phones] => SimpleXMLElement Object
(
[Phone] => Array
(
[0] => SimpleXMLElement Object
(
[PhoneType] => DDI
)
[1] => SimpleXMLElement Object
(
[PhoneType] => DEFAULT
)
[2] => SimpleXMLElement Object
(
[PhoneType] => FAX
)
[3] => SimpleXMLElement Object
(
[PhoneType] => MOBILE
)
)
)
[UpdatedDateUTC] => 2017-09-04T10:34:52.93
[IsSupplier] => false
[IsCustomer] => false
[HasAttachments] => false
)
)
)
)
Code used to get the passed data
$response = $XeroOAuth->request('GET', $XeroOAuth->url('Contacts', 'core'), array());
if ($XeroOAuth->response['code'] == 200) {
$contacts = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']);
pr($contacts);
}
I need to group this array according to proposal_num, the array with same proposal number will lies in the same row. How can i do this.. the array structure and other details are below.
Array
(
[0] => Array
(
[proposal_num] => SimpleXMLElement Object
(
[0] => 1
)
[date_departure] => SimpleXMLElement Object
(
[0] => 150715
)
[time_departure] => SimpleXMLElement Object
(
[0] => 0713
)
[date_arrival] => SimpleXMLElement Object
(
[0] => 150715
)
[time_arrival] => SimpleXMLElement Object
(
[0] => 0845
)
[departure] => SimpleXMLElement Object
(
[0] => ATL
)
[depart_terminal] => SimpleXMLElement Object
(
[0] => N
)
[arrival] => SimpleXMLElement Object
(
[0] => BWI
)
[marketing_cmpny] => SimpleXMLElement Object
(
[0] => NK
)
[operating_cmpny] => SimpleXMLElement Object
(
[0] => NK
)
[flight_number] => SimpleXMLElement Object
(
[0] => 824
)
[equipment_type] => SimpleXMLElement Object
(
[0] => 319
)
[total_fare] => SimpleXMLElement Object
(
[0] => 326.20
)
[total_tax] => SimpleXMLElement Object
(
[0] => 49.00
)
)
[1] => Array
(
[proposal_num] => SimpleXMLElement Object
(
[0] => 2
)
[date_departure] => SimpleXMLElement Object
(
[0] => 150715
)
[time_departure] => SimpleXMLElement Object
(
[0] => 1535
)
[date_arrival] => SimpleXMLElement Object
(
[0] => 150715
)
[time_arrival] => SimpleXMLElement Object
(
[0] => 1702
)
[departure] => SimpleXMLElement Object
(
[0] => ATL
)
[depart_terminal] => SimpleXMLElement Object
(
[0] => N
)
[arrival] => SimpleXMLElement Object
(
[0] => CLT
)
[marketing_cmpny] => SimpleXMLElement Object
(
[0] => AA
)
[operating_cmpny] => SimpleXMLElement Object
(
[0] => US
)
[flight_number] => SimpleXMLElement Object
(
[0] => 1711
)
[equipment_type] => SimpleXMLElement Object
(
[0] => 321
)
[total_fare] => SimpleXMLElement Object
(
[0] => 430.20
)
[total_tax] => SimpleXMLElement Object
(
[0] => 69.28
)
)
[2] => Array
(
[proposal_num] => SimpleXMLElement Object
(
[0] => 2
)
[date_departure] => SimpleXMLElement Object
(
[0] => 150715
)
[time_departure] => SimpleXMLElement Object
(
[0] => 1740
)
[date_arrival] => SimpleXMLElement Object
(
[0] => 150715
)
[time_arrival] => SimpleXMLElement Object
(
[0] => 1903
)
[departure] => SimpleXMLElement Object
(
[0] => CLT
)
[arrival] => SimpleXMLElement Object
(
[0] => BWI
)
[marketing_cmpny] => SimpleXMLElement Object
(
[0] => AA
)
[operating_cmpny] => SimpleXMLElement Object
(
[0] => US
)
[flight_number] => SimpleXMLElement Object
(
[0] => 703
)
[equipment_type] => SimpleXMLElement Object
(
[0] => 321
)
[total_fare] => SimpleXMLElement Object
(
[0] => 430.20
)
[total_tax] => SimpleXMLElement Object
(
[0] => 69.28
)
)
)
i wanted to group this array according to proposal number.
i need output like..
Airline Stops Departs Arrives Duraton Price
NK ATL→BWI 15/07/15 07:13 15/07/15 08:45 1hr 32m $326.20
US ATL→CLT 15/07/15 15:35 15/07/15 17:02 1hr 27m $430.20
US CLT→BWI 15/07/15 17:40 15/07/15 19:03 1hr 23m
please help me to solve this .. thanks in advance
I have been trying to get the Amount from the LandedPrice area of this XML that was returned. I have tried this and other code that looks similar. Would anyone have any suggestions of how I would grab that information from this?
$simplexml = new SimpleXMLElement($response); // Is response from server.
$amount = (string) $simplexml->CompetitivePrice[0]->LandedPrice->Price->Amount;
SimpleXMLElement Object
( [GetCompetitivePricingForASINResult] => SimpleXMLElement Object
( [#attributes] => Array ( [ASIN] => B00H9M9B34 [status] => Success ) [Product] => SimpleXMLElement Object
( [Identifiers] => SimpleXMLElement Object
( [MarketplaceASIN] => SimpleXMLElement Object
( [MarketplaceId] => ATVPDKIKX0DER [ASIN] => B00H9M9B34 )
) [CompetitivePricing] => SimpleXMLElement Object
( [CompetitivePrices] => SimpleXMLElement Object
( [CompetitivePrice] => SimpleXMLElement Object
( [#attributes] => Array ( [belongsToRequester] => false [condition] => New [subcondition] => New ) [CompetitivePriceId] => 1 [Price] => SimpleXMLElement Object
( [LandedPrice] => SimpleXMLElement Object
( [CurrencyCode] => USD [Amount] => 75.00 ) [ListingPrice] => SimpleXMLElement Object
( [CurrencyCode] => USD [Amount] => 75.00 ) [Shipping] => SimpleXMLElement Object
( [CurrencyCode] => USD [Amount] => 0.00 )
)
)
) [NumberOfOfferListings] => SimpleXMLElement Object
( [OfferListingCount] => Array ( [0] => 1 [1] => 1 ) )
) [SalesRankings] => SimpleXMLElement Object
( [SalesRank] => SimpleXMLElement Object
( [ProductCategoryId] => sports_display_on_website [Rank] => 625877 )
)
)
)
[ResponseMetadata] => SimpleXMLElement Object
( [RequestId] => 234234343d-bawff3f3-vef5555rrer)
)
I've been at this problem for a week or so and haven't found any information in already exiting questions that have lead to a solution. I am pulling in a very large XML file from a company (it's a MITS Feed) that easily tops a million lines of output. I unfortunately can not give the location of this file due to legal reasons, but i'll explain what I'm trying to do and maybe you all can help me put this issue to rest.
I'm calling in the file as such:
<?php
$xml=simplexml_load_file("location-of-file/feed.xml");
?>
After this I want to run through and pull out pieces of information for each property. Here is the exact schema we are using -
http://www.mitsproject.com/Content/ServeFile.cfm?FileID=4075
I've tried importing this as a DOMDocument and traversing through the XML that way, but have had no luck. Possibly just an example of how to pull all of the Property nodes in a PHP for loop would be the bomb. Thank you in advance.
P.S. I know the file is at least being grabbed, because when I run -
<?php
$xml = simplexml_load_file("location-of-file/feed.xml", null, LIBXML_NOCDATA);
print_r($xml);
?>
I am getting an insanely large output.
UPDATE:
Here is an example of one 'Property' as a SimpleXMLElement, maybe this will help -
[111] => SimpleXMLElement Object
(
[#attributes] => Array
(
[IDValue] => 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
)
)
)
try outputting the feed using
echo '<pre>';
print_r($feed);
echo '</pre>';
This should give you a much 'prettier' view of the scheme and what nodes are/aren't accessible!
I'd recommend something similar to this:
$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;
}
however please note that $item is a copy of the array $xml so to manipulate this object you will need to change $item to &$item or you will need to manipulate the actual value as such in your loop
$test = $item->WebSite;
//do something to $test
$xml->$key->WebSite = $test;
if however you are building a new array/xml document you don't need to worry about this issue
Update:
I have tested this on an example feed and you should be able to get this too work with a little editing
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();
basically give this ago, edit it to use the right nodes and build the feed right for you then this should work. the asXML() has a optional parameter you can pass in to write the file
so changing echo $new->asXML(); to $new->asXML("test.xml"); will instead write the file to test.xml instead of outputting the data. if you do save it make sure you get rid of header("Content-Type: application/xml; charset=utf-8");
I exported the .kml file for a place list created on google maps, this looks like so:
<Placemark>
<name>Name of first business here</name>
<description><![CDATA[<div dir="ltr">Address etc
<br>Tel No
<br>www.example.com</div>]]></description>
<styleUrl>#style17</styleUrl>
<Point>
<coordinates>1893891893891</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Second business</name>
<description><![CDATA[<div dir="ltr">Address here
<br>Tel no
<br>www.webaddress.com</div>]]></description>
<styleUrl>#style5</styleUrl>
<Point>
<coordinates>7317783871871</coordinates>
</Point>
</Placemark>
<Placemark>
..
</Placemark>
..
I would simply like to loop through and echo the names out to the page.
I have tried loading the file as a simplexml file and using a foreach loop to attempt to read the values, with no success.
This code prints out just the first name, doesn't appear to loop, stops after first name:
<?php
$distr = simplexml_load_file('../../media/Distributors.kml');
foreach ($distr as $value) {
echo '<li>' . $value->Placemark->name . '</li>';
}
?>
This code looks like it should work, but prints nothing:
<?php
$distr = simplexml_load_file('../../media/Distributors.kml');
foreach ($distr->Placemark as $placemark) {
echo '<li>' . $placemark->name . '</li>';
}
?>
I've tried a few other combinations but neither seem to do what I want, what am I doing wrong?
Print_r dump:
SimpleXMLElement Object
(
[Document] => SimpleXMLElement Object
(
[name] => Distributors
[description] => SimpleXMLElement Object
(
)
[Style] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style17
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => http://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png
)
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style5
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => http://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png
)
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style4
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style18
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => http://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png
)
)
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style15
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style20
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[6] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style6
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[7] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style23
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[8] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style22
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style11
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[10] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style7
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[11] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style12
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[12] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style3
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[22] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style8
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
)
[Placemark] => Array
(
[0] => SimpleXMLElement Object
(
[name] => Rigibore Ltd
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style17
[Point] => SimpleXMLElement Object
(
[coordinates] => -5.399673,50.191875,0.000000
)
)
[1] => SimpleXMLElement Object
(
[name] => Rigibore Inc.
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style5
[Point] => SimpleXMLElement Object
(
[coordinates] => -88.320183,42.867550,0.000000
)
)
[2] => SimpleXMLElement Object
(
[name] => Vibha Technologies
[styleUrl] => #style4
[Point] => SimpleXMLElement Object
(
[coordinates] => 77.568222,13.002831,0.000000
)
)
[3] => SimpleXMLElement Object
(
[name] => Centra-Tech bvba
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style18
[Point] => SimpleXMLElement Object
(
[coordinates] => 4.867222,51.141304,0.000000
)
)
[4] => SimpleXMLElement Object
(
[name] => A B T Machine Tools & Tooling Ltd
[styleUrl] => #style15
[Point] => SimpleXMLElement Object
(
[coordinates] => -4.324394,55.894394,0.000000
)
)
[5] => SimpleXMLElement Object
(
[name] => Cromwell Tools Ltd WDC
[styleUrl] => #style20
[Point] => SimpleXMLElement Object
(
[coordinates] => -1.124264,52.585289,0.000000
)
)
[6] => SimpleXMLElement Object
(
[name] => Beijing Promise
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style6
[Point] => SimpleXMLElement Object
(
[coordinates] => 116.300972,39.945293,0.000000
)
)
[7] => SimpleXMLElement Object
(
[name] => Donini E Grandi Srl Do-Gra
[styleUrl] => #style23
[Point] => SimpleXMLElement Object
(
[coordinates] => 11.331189,44.512886,0.000000
)
)
[8] => SimpleXMLElement Object
(
[name] => Euskron S.A.
[styleUrl] => #style22
[Point] => SimpleXMLElement Object
(
[coordinates] => -1.947317,43.093163,0.000000
)
)
[9] => SimpleXMLElement Object
(
[name] => H B Tools Ltd
[styleUrl] => #style11
[Point] => SimpleXMLElement Object
(
[coordinates] => -2.865015,53.601593,0.000000
)
)
[10] => SimpleXMLElement Object
(
[name] => Monks & Crane Ltd
[styleUrl] => #style7
[Point] => SimpleXMLElement Object
(
[coordinates] => -2.028771,52.547211,0.000000
)
)
[11] => SimpleXMLElement Object
(
[name] => Hudson Foster
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style12
[Point] => SimpleXMLElement Object
(
[coordinates] => -1.509982,53.665184,0.000000
)
)
[12] => SimpleXMLElement Object
(
[name] => BIM Sp. z o.o.
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style3
[Point] => SimpleXMLElement Object
(
[coordinates] => 16.772060,52.437359,0.000000
)
)
[13] => SimpleXMLElement Object
(
[name] => Precise Tooling System
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style9
[Point] => SimpleXMLElement Object
(
[coordinates] => 103.875557,1.377493,0.000000
)
)
[14] => SimpleXMLElement Object
(
[name] => Campbell Peter Sales Ltd
[styleUrl] => #style2
[Point] => SimpleXMLElement Object
(
[coordinates] => -3.949751,55.848644,0.000000
)
)
[15] => SimpleXMLElement Object
(
[name] => Obradors D R M, S.l.
[styleUrl] => #style16
[Point] => SimpleXMLElement Object
(
[coordinates] => 2.171534,41.400570,0.000000
)
)
[16] => SimpleXMLElement Object
(
[name] => Multicarb CC T/A Multitrade Distributors
[styleUrl] => #style1
[Point] => SimpleXMLElement Object
(
[coordinates] => 28.178808,-26.152639,0.000000
)
)
[17] => SimpleXMLElement Object
(
[name] => Howard Lee & Son
[styleUrl] => #style10
[Point] => SimpleXMLElement Object
(
[coordinates] => -2.062812,52.471966,0.000000
)
)
[18] => SimpleXMLElement Object
(
[name] => Glendower Cutting Tools Ltd
[styleUrl] => #style21
[Point] => SimpleXMLElement Object
(
[coordinates] => -1.105845,52.674934,0.000000
)
)
[19] => SimpleXMLElement Object
(
[name] => Finn A Værktøj A/S
[styleUrl] => #style13
[Point] => SimpleXMLElement Object
(
[coordinates] => 9.853188,54.986698,0.000000
)
)
[20] => SimpleXMLElement Object
(
[name] => Flatley.PMG
[styleUrl] => #style14
[Point] => SimpleXMLElement Object
(
[coordinates] => -6.337717,53.312447,0.000000
)
)
[21] => SimpleXMLElement Object
(
[name] => Iscar Tools Argentina Sa
[styleUrl] => #style19
[Point] => SimpleXMLElement Object
(
[coordinates] => -58.404968,-34.639957,0.000000
)
)
[22] => SimpleXMLElement Object
(
[name] => Duroc Machine Tool AB
[styleUrl] => #style8
[Point] => SimpleXMLElement Object
(
[coordinates] => 18.092628,59.449158,0.000000
)
)
)
)
)
If you want to read xml file and loop through the xml elments .try something like this
<songs>
<song dateplayed="2011-07-24 19:40:26">
<title>I left my heart on Europa</title>
<artist>Ship of Nomads</artist>
</song>
<song dateplayed="2011-07-24 19:27:42">
<title>Oh Ganymede</title>
<artist>Beefachanga</artist>
</song>
<song dateplayed="2011-07-24 19:23:50">
<title>Kallichore</title>
<artist>Jewitt K. Sheppard</artist>
</song>
</songs>
<?php
$dir = "home/";
$files = glob($dir."*.xml");
foreach($files as $file_xml)
{
$headernode = new SimpleXMLElement($file_xml,null,true);
foreach($headernode as $property)
{
echo $name = $property->first;
}
}
OR try something like this
$mysongs = simplexml_load_file('songs.xml');
echo $mysongs->song[0]->artist;
?>