How to add namespace with SimpleXML in PHP [duplicate] - php

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

Related

PHP Trying to Get Simple XML Attribute Value [duplicate]

This question already has answers here:
Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?
(2 answers)
Closed 4 years ago.
Here is my XML:
<WebContent diffgr:id="WebContent1" msdata:rowOrder="0">
<orig_inv_no>73</orig_inv_no>
<inv_no>141</inv_no>
<inv_type>S</inv_type>
<content_type>3</content_type>
<content_type_desc>Test</content_type_desc>
<content_value>Sample content</content_value>
</WebContent>
<WebContent diffgr:id="WebContent2" msdata:rowOrder="0">
<orig_inv_no>73</orig_inv_no>
<inv_no>141</inv_no>
<inv_type>S</inv_type>
<content_type>3</content_type>
<content_type_desc>Test</content_type_desc>
<content_value>Sample content</content_value>
</WebContent>
I am having a lot of trouble getting the attribute "differ:id" for the node "WebContent"
It seems like it doesn't like the colon in the attribute name. Any Ideas?
the attribute is "diffgr:id" not "differ:id" maybe this is your issue
Try this:
$xml->WebContent->attributes("diffgr",TRUE)->id;
// TRUE means that `diffgr` is a prefix of the attribute
COuld be found here -> https://stackoverflow.com/a/15546669/2040840

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

Get DOM element attribute, using php [duplicate]

This question already has answers here:
PHP: DomElement->getAttribute
(4 answers)
Closed 9 years ago.
If I have a PHP variable that is a string such as this:
$link = "<a href='test.jpg'>Download</a>";
How can I use the variable $link to get the value for href using PHP and not javascript?
If the text big, you can use some library as phpQuery, Zend DOM Query, Nokogiri or other. Otherwise, use regexp.

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;
}

PHP SimpleXML has Child [duplicate]

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

Categories