I just want to get the value from xml node.So I following the code from php document:
SimpleXMLElement::xpath() .But it didn't.And I thought the Xpath is much more inconvenience ,is there a much better way to get the node I want??!
my php code:
<?php
/**
* #author kevien
* #copyright 2010
*/
$arr = array ();
$xml = simplexml_load_file("users.xml");
$result = $xml->xpath('/users/user[#id="126"]/watchHistory/whMonthRecords[#month="2010-09"]/whDateList/date');
while(list( , $node) = each($result)) {
array_push($arr, $node);
}
print_r($arr);
?>
it returns:
Array ( [0] => SimpleXMLElement Object ( [0] => 02 ) [1] => SimpleXMLElement Object ( [0] => 03 ) [2] => SimpleXMLElement Object ( [0] => 06 ) [3] => SimpleXMLElement Object ( [0] => 10 ) [4] => SimpleXMLElement Object ( [0] => 21 ) )
my part of users.xml :
<users>
<user id="126">
<name>老黄牛三</name>
<watchHistory>
<whMonthRecords month="2010-09">
<whDateList month="2010-09">
<date>02</date>
<date>03</date>
<date>06</date>
<date>10</date>
<date>21</date>
</whDateList>
</<whMonthRecords>
</<watchHistory>>
</user>
</users>
Thank you very much!!
Replace your whole loop with:
foreach ($result as $node) {
$arr[] = (string)$node;
}
or even:
$result = array_map('strval', $result);
Related
I can't manage to sort an array alfabetically.
It's an array with cities that I get from an external XML.
The XML looks like this, and it's the node localidad I am trying to sort.
<parada>
<id>506</id>
<localidad>
<![CDATA[ Alvor ]]>
</localidad>
<parada>
<![CDATA[ Alvor Baia Hotel (Bus Stop Alvor Férias) ]]>
</parada>
<lat>37.1296</lat>
<lng>-8.58058</lng>
<horasalida>05:40</horasalida>
</parada>
The relevant code:
$xml = new SimpleXMLElement($viajes);
foreach ($xml->parada as $excursion) {
$newParadasarray[] = $excursion->localidad;
}
$newParadasarray = array_unique($newParadasarray);
foreach ($newParadasarray as $parada) {
if (strpos($parada, 'Almuñecar') !== false)
echo '<option value="Almuñecar">Almuñecar</option>';
if (strpos($parada, 'Benalmádena') !== false)
echo '<option value="Benalmádena Costa">Benalmádena Costa</option>';
if (strpos($parada, 'Estepona') !== false)
echo '<option value="Estepona">Estepona</option>';
etc.
}
I have tried with sort() and array_values().
This is the output of print_r($newParadasarray):
Array (
[0] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
[1] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
[2] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
[4] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
[9] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
[14] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
[20] => etc.
The problem is that your assigning a SimpleXMLElement into the array, instead you want the content of the element, so just change the line...
$newParadasarray[] = $excursion->localidad;
to
$newParadasarray[] = trim((string)$excursion->localidad);
The cast (string) takes the text content and trim() removes the extra whitespace around it.
I am assuming that you have multiple <parada> elements, so that $xml->parada is returning the correct data.
If you're familiar with DOMDocument you could simply do this:
$doc = new DOMDocument();
$doc->loadXML($xml);
$array = array();
foreach($doc->getElementsByTagName("localidad") as $localidad) {
$array[] = trim($localidad->nodeValue);
}
$array = array_unique($array);
sort($array);
Given a deep array, how do you display line-delimited values from it?
Array (
[0] => Array (
[godzina] => SimpleXMLElement Object (
[0] => 17:00:00
)
[data] => SimpleXMLElement Object (
[0] => 2015-09-02
)
[kurs] => SimpleXMLElement Object (
[0] => 2.03
)
)
)
This is the output I'm expecting:
hours: 17:00:00
date: 2015-09-02
exchange: 2.03
I get this array from an xml, via curl, so the source can't be altered.
Figured Out:
$h = 'godzina';
$hvalue = $zmt[$h];
$d = 'data';
$dvalue = $zmt[$d];
$e = 'kurs';
$evalue = $zmt[$e];
and then echo with [0].
BUT thanks. For foreach loop.
Assuming your array is in a variable $myArr, you should be able to do a simple foreach:
foreach ($myArr[0] as $k => $v) {
echo $k.": ".$v[0]."\n"
}
I have a SimpleXML Object made from merging multiple XMLs from PubMed (snippet below) but there is repetition from the merge. How can I compare all first child arrays - array[][0], array[][1] etc - and discard any duplicates?
I though perhaps serialising was the answer but you can't serialise a SimpleXML Object afaik..
I'm not sure where to start?
Array
(
[0] => Array
(
[title] => SimpleXMLElement Object
(
[0] => Superstructure of the centromeric complex of TubZRC plasmid partitioning systems.
)
[link] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Version] => 1
)
[0] => 23010931
)
[author] => Aylett, CH., Löwe, J.
[journal] => SimpleXMLElement Object
(
[0] => Proc. Natl. Acad. Sci. U.S.A.
)
[pubdate] => 2012-9-27
[day] => SimpleXMLElement Object
(
[0] => 25
)
[month] => SimpleXMLElement Object
(
[0] => Sep
)
[year] => SimpleXMLElement Object
(
[0] => 2012
)
)
[1] => Array
(
[title] => SimpleXMLElement Object
(
[0] => Superstructure of the centromeric complex of TubZRC plasmid partitioning systems.
)
[link] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Version] => 1
)
[0] => 23010931
)
[author] => Aylett, CH., Löwe, J.
[journal] => SimpleXMLElement Object
(
[0] => Proc. Natl. Acad. Sci. U.S.A.
)
[pubdate] => 2012-9-27
[day] => SimpleXMLElement Object
(
[0] => 25
)
[month] => SimpleXMLElement Object
(
[0] => Sep
)
[year] => SimpleXMLElement Object
(
[0] => 2012
)
)
Alternatively it could be done at the initial XML merge stage - I use the code below at the moment if anyone can suggest how to modify it to remove duplicates?
function simplexml_merge (SimpleXMLElement &$xml1, SimpleXMLElement $xml2) {
$dom1 = new DomDocument();
$dom2 = new DomDocument();
$dom1->loadXML($xml1->asXML());
$dom2->loadXML($xml2->asXML());
$xpath = new domXPath($dom2);
$xpathQuery = $xpath->query('/*/*');
for ($i = 0; $i < $xpathQuery->length; $i++) {
$dom1->documentElement->appendChild(
$dom1->importNode($xpathQuery->item($i), true));
}
$xml1 = simplexml_import_dom($dom1);
}
$xml1 = new SimpleXMLElement($search1);
$xml2 = new SimpleXMLElement($search2);
simplexml_merge($xml1, $xml2);
Thanks.
...
...
For clarity - here's the XML source layout that I am importing into SimpleXML - each PubmedArticle is one "element" I am interested in comparing and ensuring there are no duplicates -
<xml...>
<Document>
<PubmedArticle>
<MedlineCitation>
<PMID version="1">xxx</PMID>
...
</MedlineCitation>
...
</PubmedArticle>
<PubmedArticle>
<MedlineCitation>
<PMID version="1">xxx</PMID>
...
</MedlineCitation>
...
</PubmedArticle>
etc
</Document>
</xml>
The PMID node is unique so can be used to check for duplicates.
...
...
Using the link from #Gordon - I know use:
//Get my source XML
$xml1 = new SimpleXMLElement($search1);
$xml2 = new SimpleXMLElement($search2);
//Run through $xml1 and build a query based on it's PMIDs
$query = array();
foreach ($xml1->PubmedArticle as $paper) {
$query[] = sprintf('(PMID != %s)',$paper->MedlineCitation->PMID);
}
$query = implode('and', $query);
//Run through $xml2 and get node which don't have PMID matching $xml1
foreach ($xml2->xpath(sprintf('PubmedArticle/MedlineCitation[%s]', $query)) as $paper) {
echo $paper->asXml();
}
However I still have one problem - getting the output merged.
The output of $xml2 is missing the <PubmedArticle> node around each 'match' for a start. Then I presume I can use the same merge code (above) to do the merge.
Can you point me in the right direction?
Convert it to an array (which I'm not going to write for you, just iterate and add.), then array_diff().
Decided to follow #Gordon's line as it kept it XML. Eventually got it all working:
//function to check 2 xml inputs for duplicate nodes
function dedupeXML($xml1, $xml2) {
$query = array();
foreach ($xml1->PubmedArticle as $paper) {
$query[] = sprintf('(MedlineCitation/PMID != %s)',$paper->MedlineCitation->PMID);
}
$query = implode('and', $query);
$xmlClean = '<Document>';
foreach ($xml2->xpath(sprintf('PubmedArticle[%s]', $query)) as $paper) {
$xmlClean .= $paper->asXML();
}
$xmlClean .= '</Document>';
$xmlClean = new SimpleXMLElement($xmlClean);
return $xmlClean;
}
//function to merge 2 xml inputs
function mergeXML (SimpleXMLElement &$xml1, SimpleXMLElement $xml2) {
// convert SimpleXML objects into DOM ones
$dom1 = new DomDocument();
$dom2 = new DomDocument();
$dom1->loadXML($xml1->asXML());
$dom2->loadXML($xml2->asXML());
// pull all child elements of second XML
$xpath = new domXPath($dom2);
$xpathQuery = $xpath->query('/*/*');
for ($i = 0; $i < $xpathQuery->length; $i++) {
// and pump them into first one
$dom1->documentElement->appendChild(
$dom1->importNode($xpathQuery->item($i), true));
}
$xml = simplexml_import_dom($dom1);
return $xml;
}
$xml1 = new SimpleXMLElement($search1);
$xml2 = new SimpleXMLElement($search2);
$xml3 = new SimpleXMLElement($search3);
//dedupe and merge inputs
//input 1 & 2
$xml2Clean = dedupeXML($xml1, $xml2);
$xml12 = mergeXML($xml1, $xml2Clean);
//input 1+2 & 3
$xml3Clean = dedupeXML($xml12, $xml3);
$xml123 = mergeXML($xml12, $xml3Clean);
This would be easy to adapt to other data sources - just modify the dedupeXML function to match the data structure of your XML.
I need to parse XML string into an array.
I have XML
<group xmlns="123" id="personal">
<field id="last_name">last</field>
<field id="first_name">first</field>
<field id="birth_day">10/10/1990</field>
<field id="gender"/>
</group>
I'm using SimpleXML in php
$obj = simplexml_load_string($xml_string);
var_dump($obj);
SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => personal
)
[field] => Array
(
[0] => first
[1] => last
[2] => 10/10/1990
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => gender
)
)
)
)
how to get such array? is it possible?
[field] => Array(
[last_name] => last,
[first_name] => first,
[birth_day] => 10/10/1990,
[gender] => NULL,
....
)
I do not know how else to explain this situation.
I want to index the id attribute value were.
please help.
Simple as that (Note it uses PHP 5.3 features):
libxml_use_internal_errors(true); // Turn off warnings because of invalid '123' namespace
$obj = simplexml_load_string($xml);
$obj->registerXPathNamespace('ns', '123');
$fields = $obj->xpath('//ns:field');
libxml_clear_errors(); // Clear warnings buffer
$result = array();
array_walk($fields, function($el) use (&$result) {
$result[(string)$el['id']] = (string)$el ?: null;
});
echo '<pre>'; print_r($result); echo '</pre>';
Output (null value is not shown by print_r):
Array
(
[last_name] => last
[first_name] => first
[birth_day] => 10/10/1990
[gender] =>
)
Try this,
print_r((array)($obj));
Check examples at simple_xml_load
You can go over your XML like this:
foreach ($obj->field as $field) {
...
}
$field will be the array containing last_name, first_name, birthday and gender.
Here is my code:
$string = <<<XML
<?xml version='1.0'?>
<test>
<testing>
<lol>hello</lol>
<lol>there</lol>
</testing>
</test>
XML;
$xml = simplexml_load_string($string);
echo "All of the XML:\n";
print_r $xml;
echo "\n\nJust the 'lol' array:";
print_r $xml->testing->lol;
Output:
All of the XML:
SimpleXMLElement Object
(
[testing] => SimpleXMLElement Object
(
[lol] => Array
(
[0] => hello
[1] => there
)
)
)
Just the 'lol' array:
SimpleXMLElement Object
(
[0] => hello
)
Why does it output only the [0] instead of the whole array? I dont get it.
What #Yottatron suggested is true, but not at all the cases as this example shows :
if your XML would be like this:
<?xml version='1.0'?>
<testing>
<lol>
<lolelem>Lol1</lolelem>
<lolelem>Lol2</lolelem>
<notlol>NotLol1</lolelem>
<notlol>NotLol1</lolelem>
</lol>
</testing>
Simplexml's output would be:
SimpleXMLElement Object
(
[lol] => SimpleXMLElement Object
(
[lolelem] => Array
(
[0] => Lol1
[1] => Lol2
)
[notlol] => Array
(
[0] => NotLol1
[1] => NotLol1
)
)
)
and by writing
$xml->lol->lolelem
you'd expect your result to be
Array
(
[0] => Lol1
[1] => Lol2
)
but instead of it, you would get :
SimpleXMLElement Object
(
[0] => Lol1
)
and by
$xml->lol->children()
you would get:
SimpleXMLElement Object
(
[lolelem] => Array
(
[0] => Lol1
[1] => Lol2
)
[notlol] => Array
(
[0] => NotLol1
[1] => NotLol1
)
)
What you need to do if you want only the lolelem's:
$xml->xpath("//lol/lolelem")
That gives this result (not as expected shape but contains the right elements)
Array
(
[0] => SimpleXMLElement Object
(
[0] => Lol1
)
[1] => SimpleXMLElement Object
(
[0] => Lol2
)
)
It's because you have two lol elements. In order to access the second you need to do this:
$xml->testing->lol[1];
this will give you "there"
$xml->testing->lol[0];
Will give you "hello"
The children() method of the SimpleXMLElement will give you an object containing all the children of an element for example:
$xml->testing->children();
will give you an object containing all the children of the "testing" SimpleXMLElement.
If you need to iterate, you can use the following code:
foreach($xml->testing->children() as $ele)
{
var_dump($ele);
}
There is more information about SimpleXMLElement here:
http://www.php.net/manual/en/class.simplexmlelement.php
what you might want to do is using the Json encode/decode
$jsonArray = Json_decode(Json_encode($xml), true);
With the true argument you can call instead of using -> use [name]
so an example would be:
$xml = file("someXmlFile.xml");
$jsonArray = Json_decode(Json_encode($xml), true);
foreach(jsonArray['title'] as $title){
Do something with $titles
}
if you have more than 1 element it will typical put in an #attributes if the elements has attributes. This can be countered by using: $title = $title['#attributes']
Hope it could help.
Ah yes, I remember simple XML nearly doing my head in with this parsing arrays issue
Try the below code. It will give you an array of LOL elements, or, if you've just got a single LOL element, it will return that in an array as well.
The main advantage of that is you can do something like foreach ($lol as $element) and it will still work on a single (or on 0) LOL element.
<?php
$string = <<<XML
<?xml version='1.0'?>
<test>
<testing>
<lol>hello</lol>
<lol>there</lol>
</testing>
</test>
XML;
$xml = simplexml_load_string($string);
echo "<pre>";
echo "All of the XML:\n";
print_r($xml);
echo "\n\nJust the 'lol' array:\n";
$test_lols = $xml->testing->children();
$childcount = count($test_lols);
if ($childcount < 2) {
$lol = array($test_lols->lol);
}
else {
$lol = (array) $test_lols;
$lol = $lol['lol'];
}
print_r($lol);
?>
Ran into this issue...
Xpath can be a little slow, so you can achieve the same effect with a simple for loop.
for ($i = 0; $i < count($xml->testing->lol); $i++) {
print_r($xml->testing->lol[$i]);
}