PHP SimpleXML has Child [duplicate] - php

This question already has answers here:
php SimpleXML check if a child exists
(17 answers)
Closed 9 years ago.
How can check if $xml has child "students"?
I'm using SimpleXML to use Xml in php:
$xml = simplexml_load_file('log.xml');
Thanks in advance.

You can use a simple isset() call for that
$node = new SimpleXMLElement('<foo><students>test</students></foo>');
var_dump(isset($node->students));
bool(true)

if( isset($xml->students) )
// Do

Related

Read xml Node Values In Php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 8 years ago.
<root>
<status>Call Triggered Successfully</status>
<is_ndnc>no</is_ndnc>
<callid>aaac5814-400f-45ea-9235-f0198e5edd4b</callid>
</root>
how can Read nodes i hv to assign to variable
like
$status='Call Triggered Successfully';
$is_ndnc='no';
$callid='aaac5814-400f-45ea-9235-f0198e5edd4b';
There are many ways in which you can gt the nodevalue from xml in php.
You could use xpath which returns an array of SimpleXMLElement objects.
Or you could load the file with simplexml_load_file() and use the foreach loop as explained here

How to add namespace with SimpleXML in PHP [duplicate]

This question already has answers here:
simple xml add namespaced child
(2 answers)
Closed 5 years ago.
I will have to produce this XML using simpleXML in php:
<fr:program name="fundref">
<fr:assertion name="funder_name">ABC Inc.
<fr:assertion name="funder_identifier">http://dx.doi.org/10.13039/xxxxxxxxxx</fr:assertion>
</fr:assertion>
<fr:assertion name="award_number">BXDFSDS</fr:assertion>
</fr:program>
I tried:
$fundRef = $myXML->addChild('fr', '', 'program');
But this is creating:
<fr xmlns="program" name="fundref">
Thank you.
You need to determine namespace like this
$myXML->addChild('fr:program', '', 'http://ololo.com/ns/1.0');
This should help PHP's SimpleXML: How to use colons in names

PHP. XML parsing from variable [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I've such piece of code:
$xml=”<response>
<version>1.2</version>
<merchant_id></merchant_id>
<order_id> ORDER_123456</order_id>
<amount>1.01</amount>
<currency>UAH</currency>
<description>Comment</description>
<status>success</status>
<code></code>
<transaction_id>31</transaction_id>
<pay_way>card</pay_way>
<sender_phone>+3801234567890</sender_phone>
<goods_id>1234</goods_id>
<pays_count>5</pays_count>
</response>";
Could I parse it? How to do it? I had never work with XML.
E.g how to take ?
You should use simplexml_load_string function.
Parse as follow:
echo simplexml_load_string($xml)->version;
There is plenty functions you could use have a look at the link above.
Try saving your XML to a file and use simplexml to parse it in php.
See tutorial here: http://blog.teamtreehouse.com/how-to-parse-xml-with-php5
You should use DomDocument. It is much more flexible and useful than SimpleXML.
Parse as follows:
$doc = new DOMDocument();
$doc->loadXML($xml);
$price = $doc->getElementsByTagName('amount');
$currency = $doc->getElementsByTagName('currency');

How to read in XML strings as variables? [duplicate]

This question already has answers here:
A simple program to CRUD node and node values of xml file [closed]
(2 answers)
Closed 8 years ago.
Try this query:
http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da
How do I simply read in the suggestions as variables? I can't seem to find anything that explains this.
You can use SimpleXmlIterator. That's really easy to use and you will be able to perform a foreach on the object you will get.
Library source
For example with file_get_contents or replace with curl if you prefer:
$feed = new SimpleXmlIterator(file_get_contents('http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da'));
foreach ($feed->suggestion as $suggestion) {
echo $value;
}

getElementByAttribute PHP DOM [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Select xml node by attribute in php
Is there any function like getElementByAttribute in PHP? If no, how do I create a workaround?
E.g.
<div class="foo">FOO!</div>
How do I match that element?
You can use XPath:
$xpath = new DOMXPath($document);
$results = $xpath->query("//*[#class='foo']");
Here's a demo.

Categories