I have a xml file output.xml
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<key type="your_id">CYBEX-525A/DA-IPOD</key>
<key type="web">cybex-525at-arc-trainer</key>
<key type="web">standard-console-2573</key>
<key type="name">Cybex 525AT Arc Trainer</key>
<key type="name">Standard console</key>
<review>
<order_id>1544346 1</order_id>
<author_nick>Brock</author_nick>
<author_email>bb@GMAIL.COM</author_email>
<date type="accepted">2013-10-14</date>
<comment type="overall">This cardio machine is exceptional. It works every part of your leg muscles if you rotate through the height settings and include calf-raises and squats during your routine. It also works your shoulders and biceps if you focus on working them while operating the arm poles. Unlike a standard elliptical it will raise your heart rate and cause you to sweat heavily soon after you start your routine. If you're a runner and are used to using a treadmill, you will feel satisfied after using this machine. It is kind of addictive because your body feels so good during and after use. I have combined 30 minutes on the treadmill with 30 minutes on the Arc for weight-loss, muscle tone, and cardiovascular training.</comment>
<score type="overall">5</score>
</review>
</item>
</items>
I need to save it in db, I'm just using below code to save data
if(file_exists('output.xml')){
$languages = simplexml_load_file("output.xml");
//echo '<pre>'; print_r($languages) ; die;
foreach($languages as $item){
echo '<pre>'; print_r($item->key) ; die;
foreach($item->review as $review){
$order_id = $review[0]->order_id;
$authorName = $review[0]->author_nick;
$authorEmail = strtolower($review[0]->author_email);
$comment = $review[0]->comment;
$score = $review[0]->score;
$date = $review[0]->date;
}
}
}
I need to get value of <key type="your_id">CYBEX-525A/DA-IPOD</key>
and <key type="web">cybex-525at-arc-trainer</key>
but unable to get data when i print echo '<pre>'; print_r($item->key) ; die;
echo '<pre>'; print_r($item->key) ; die;
within loop I'm getting following out put:
SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => your_id
)
[0] => CYBEX-525A/DA-IPOD
[1] => cybex-525at-arc-trainer
[2] => standard-console-2573
[3] => Cybex 525AT Arc Trainer
[4] => Standard console
)
Is there any method for get all these data.
If you won't use DOMDocument, you can try :
foreach($languages->item as $item){ //loop through item(s)
foreach($item->key as $key) { //loop through key(s)
if ($key["type"] == "your_id") echo $key; //echo CYBEX-525A/DA-IPOD
}
print_r($item->review); //prints review data
}
DOMXpath::evaluate()
allows you to use expression to fetch nodes and scalar values from an XML DOM. The expression defines if the result is a node list or a scalar value. You can iterate a node list with foreach()
.
$document = new DOMDocument();
$document->loadXml($xmlString);
$xpath = new DOMXPath($document);
foreach ($xpath->evaluate('/items/item') as $item) {
var_dump(
[
'id' => $xpath->evaluate('string(key[@type="your_id"])', $item)
]
);
foreach ($xpath->evaluate('review', $item) as $review) {
var_dump(
[
'nick' => $xpath->evaluate('string(author_nick)', $review),
'email' => $xpath->evaluate('string(author_email)', $review)
]
);
}
}
Output:
array(1) {
["id"]=>
string(18) "CYBEX-525A/DA-IPOD"
}
array(2) {
["nick"]=>
string(5) "Brock"
["email"]=>
string(12) "bb@GMAIL.COM"
}
The second argument or DOMXpath::evaluate()
is a context for the expression. So it is easy to iterate a list of nodes and fetch data for them.
Xpath functions like string()
case the first node of a list into a scalar value. If the list was empty, and empty value of the type will be returned.
SimpleXML allows you fetch arrays of nodes using SimpleXMLElement::xpath()
. Here is no direct way to fetch scalar values, but the implemented magic methods allow a compact syntax.
You will have to cast the returned SimpleXMLElement objects into strings.
$items = new SimpleXMLElement($xmlString);
foreach ($items->xpath('item') as $item) {
var_dump(
[
'id' => (string)$item->xpath('key[@type="your_id"]')[0]
]
);
foreach ($item->xpath('review') as $review) {
var_dump(
[
'nick' => (string)$review->author_nick,
'email' => (string)$review->author_email
]
);
}
}
You can use as example
$xml="".$rs_rssfeed['rss_url'].""; $xmlDoc = new DOMDocument(); $xmlDoc->load($xml); $xpath = new DOMXPath($xmlDoc); $count = $xpath->evaluate('count(//item)'); if($count>0) { $x=$xmlDoc->getElementsByTagName('item'); for ($i=0; $i<$count = $xpath->evaluate('count(//item)'); $i++) { $item_title=$x->item($i)->getElementsByTagName('title')->item(0)->nodeValue; $item_link=$x->item($i)->getElementsByTagName('link')->item(0)->nodeValue; $item_desc=$x->item($i)->getElementsByTagName('description')->item(0)->nodeValue; $item_pubdate=$x->item($i)->getElementsByTagName('pubDate')->item(0)->nodeValue; } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.