Loop through xml array - php

Below is my code and I want to extract information of[field] => Array of index [1].It means [1] => 1030.670044.
This is xml file,and I want to store in my database,but unable to loop this in php.
Please Help me.
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 1.0
)
[meta] => SimpleXMLElement Object
(
[type] => resource-list
)
[resources] => SimpleXMLElement Object
(
[#attributes] => Array
(
[start] => 0
[count] => 168
)
[resource] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[classname] => Quote
)
[field] => Array
(
[0] => USD/KRW
[1] => 1030.670044
[2] => KRW=X
[3] => 1398752590
[4] => currency
[5] => 2014-04-29T06:23:10+0000
[6] => 0
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[classname] => Quote
)
[field] => Array
(
[0] => SILVER 1 OZ 999 NY
[1] => 0.051059
[2] => XAG=X
[3] => 1398721323
[4] => currency
[5] => 2014-04-28T21:42:03+0000
[6] => 400
)
)

This is a SimpleXML object. If the variable is called $xml you can access the field[1] like this:
foreach ($xml->resources->resource as $res) {
var_dump( $res->field[1] );
}

Related

How to use a variable string with array in a foreach loop

I try to get the following code working.
foreach ($uniqueItems as $key => $value) {
$output = "{$value->properties->property[10]->value}";
echo $output;
}
In the browser I see 24.99
Above code gives me the correct output. But I need the $output from outside this foreach. I try the following
$output = "{\$value->properties->property[10]->value}";
foreach ($uniqueItems as $key => $value) {
echo $output;
}
In the browser I see {$value->properties->property[10]->value}
This is print_r($uniqueItems[$key]);
SimpleXMLElement Object
(
[name] => Donnay joggingbroek zwart unisex
[properties] => SimpleXMLElement Object
(
[property] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => deliveryTime
)
[value] => Voor 16.00 uur besteld, morgen in huis!
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => brand
)
[value] => Donnay
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => size
)
[value] => SimpleXMLElement Object
(
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => color
)
[value] => Zwart
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => EAN
)
[value] => SimpleXMLElement Object
(
)
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => categoryPath
)
[value] => Tenniskleding/Tenniskleding dames
)
[6] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => deliveryCosts
)
[value] => 4.95
)
[7] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => discount
)
[value] => 5.00
)
[8] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => subcategories
)
[value] => Tenniskleding dames
)
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => SKU
)
[value] => 489000-TL-020
)
[10] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => fromPrice
)
[value] => 24.99
)
))
How to get the right output?
The second snippet won't work as php does not know that $value is to be interpolated until $value gets defined which is happening when the loop actually starts. Till then, it will just be a string. That's why it's just printing out it as a string

PHP array to take out array value only one field

I have an array where i want only the one Field text from all array which is text only. i want all text from there.
stdClass Object
(
[language] => en
[textAngle] => 0
[orientation] => Up
[regions] => Array
(
[0] => stdClass Object
(
[boundingBox] => 81,63,1340,1055
[lines] => Array
(
[0] => stdClass Object
(
[boundingBox] => 321,63,855,117
[words] => Array
(
[0] => stdClass Object
(
[boundingBox] => 321,63,174,94
[text] => Set
)
[1] => stdClass Object
(
[boundingBox] => 529,87,126,69
[text] => an
)
[2] => stdClass Object
(
[boundingBox] => 693,65,483,115
[text] => example.
)
)
)
[1] => stdClass Object
(
[boundingBox] => 218,182,1059,116
[words] => Array
(
[0] => stdClass Object
(
[boundingBox] => 218,182,271,92
[text] => Treat
)
[1] => stdClass Object
(
[boundingBox] => 521,203,504,95
[text] => everyOne
)
[2] => stdClass Object
(
[boundingBox] => 1054,182,223,91
[text] => With
)
)
)
I want take out from here like [text]=>Set,[text]=>an,[text]=>example.
eg set an example.
Output should be eg. only like set an example
Given your example class above, I would try something like this.
$text = '';
foreach ($class->regions[0]->lines as $line){
foreach ($line->words as $word){
$text = $text." ".$word->text;
}
}
print $text;

How to loop through an array of SimpleXMLElement objects and return node default value?

I am trying to get default value for each field but I just can't figure out how to loop through all the objects. Tried with converting them to a simple array with json_decode but it just not clear as to what to loop.
Here it is:
SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => params
)
[fieldset] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Cat1
)
[field] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] =>
[type] => list
[default] => 1
)
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Item2
)
[field] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post1
[type] => text
[default] => 5
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post2
[type] => text
[default] => 18
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post3
[type] => text
[default] => 15
)
[option] => Array
(
[0] => Blue
[1] => Green
)
)
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Cat2
)
[field] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post6
[type] => text
[default] => 3
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post7
[type] => text
[default] => 36
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post7
[type] => text
[default] => 88
)
)
)
)
)
Try this (where $xml is your root object)
foreach($xml->fieldset as $fieldset) {
foreach($fieldset->field as $field) echo (string)$field['default'];
}

Using a PHP for loop to display repeating nodes from a huge external XML feed

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");

parse xml to mysql

I have a XML feed from which I need to parse some of it and store it into MySQL table, but I'm facing a new kind of XML (I'm not good at this)
Here is the XML I'm getting which i'm opening using simplexml_load_file from this address:
It's a pubmed journal publication I need to store authorlist and some other, but with a hint on how to access it I can manage.
Thanks a lot
SimpleXMLElement Object
(
[DocSum] => SimpleXMLElement Object
(
[Id] => 22064119
[Item] => Array
(
[0] => 2011 Dec
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => EPubDate
[Type] => Date
)
)
[2] => Clin Nucl Med
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => AuthorList
[Type] => List
)
[Item] => Array
(
[0] => Jackson TA
[1] => Choong KW
[2] => Eng JA
[3] => McAneny D
[4] => Subramaniam RM
[5] => Knapp PE
)
)
[4] => Knapp PE
[5] => F-18 FDG PET/CT Imaging of Endogenous Cushing Syndrome.
[6] => 36
[7] => 12
[8] => e231-2
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => LangList
[Type] => List
)
[Item] => English
)
[10] => 7611109
[11] => 0363-9762
[12] => 1536-0229
[13] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => PubTypeList
[Type] => List
)
[Item] => Journal Article
)
[14] => PubMed - in process
[15] => ppublish
[16] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => ArticleIds
[Type] => List
)
[Item] => Array
(
[0] => 10.1097/RLU.0b013e3182336360
[1] => 00003072-201112000-00044
[2] => 22064119
[3] => 22064119
[4] => 22064119
)
)
[17] => 10.1097/RLU.0b013e3182336360
[18] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => History
[Type] => List
)
[Item] => Array
(
[0] => 2011/11/09 06:00
[1] => 2011/11/09 06:00
[2] => 2011/11/09 06:00
)
)
[19] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => References
[Type] => List
)
)
[20] => 1
[21] => 0
[22] => Clinical nuclear medicine
[23] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => ELocationID
[Type] => String
)
)
[24] => 2011 Dec;36(12):e231-2
)
)
)
You can run an XPath on the object like this: $xml->xpath('/DocSum/Item/3/Item');

Categories