PHP Trying to Get Simple XML Attribute Value [duplicate] - php

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

Related

Simple XML read element namespace attribute [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.
I have an XML, schema below
$xml =
'<NodeSet
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://opcfoundation.org/UA/2008/02/Types.xsd">
<Node category="category" i:type="ObjectNode">
<NodeId>
<Identifier>i=86</Identifier>
</NodeId>
</Node>
</NodeSet>';
I need to extract the i:type attribute value from the Node element. I have tried accessing it as i would do it when accessing normal attributes but it seems it doesn't work that way
Here is how i can access the category attribute,
$xml=simplexml_load_string($xml);
echo $xml->Node[0]['category']; //this prints 'category' as expected
echo $xml->Node[0]['i:type']; //prints nothing, how do i get the i:type attribute value ?
You need to access the attributes with the namespace, this can be done using the attributes() method...
echo $xml->Node[0]->attributes("i",true)['type'];
Using ("i",true) says use the i prefix rather than having to put the URI.

Dynamic prefix attribute PHP [duplicate]

This question already has an answer here:
How can I access a property with an invalid name?
(1 answer)
Closed 6 years ago.
I have a question about attributes in PHP.
I have various Class with the same attributes, but with with different prefix.
Example:
$attr->a_field;
$attr2->b_field;
So, with another Class I want to access to them.
I tried:
$field = "{$prefix}_field";
$attr->{$field}
and it works perfect. But is any other way to doing this?
I tried also with:
$attr->{$prefix}_field;
$attr->{$prefix}{"_field"};
$attr->"{$prefix}_field";
etc and who I suppose I get PHP's errors
Thanks!
You can write it directly as $attr->{"{$prefix}_field"}, as shown in the docs.
You're looking into variable variables
$attr->{$prefix."_field"}

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

How to get XML nodes content when names include special Characters? [duplicate]

This question already has answers here:
Simple XML - Dealing With Colons In Nodes
(4 answers)
Closed 9 years ago.
Im trying to navigate an XML block similar to this one ($doc) using PHP simplexml_load_string and using xpath on $doc to get only the 'Day' block like this:
$myday = $doc->xpath ('//Day');
that lets me access all data from the block as an object, meaning
$myday->AdultCount;
returns 1 and
$myday->Id;
returns "6a0"
however I can't access "SpecialDeals" content not using:
$myday->SpecialDeals
nor using:
$myday->SpecialDeals->a:string
Whats is the right syntax in this case?
<Days>
<DaysId>687</DaysId>
<Day>
<AdultsCount>1</AdultsCount>
<Availability>Available</Availability>
<Id>6a0</Id>
<RoomType>Studio</RoomType>
<SpecialDeals xmlns:a="http://microsoft.com/2003/Arrays">
<a:string>Best Day Ever</a:string>
</SpecialDeals>
</Day>
<DaysPrice>247.4</DaysPrice>
</Days>");
You can access the tags with colons in them (aka namespaces) using the children() method:
echo $xml->Day->SpecialDeals->children('a', true)->string[0];
Demo!
This SitePoint article explains namespaces in detail.

Categories