Get an attribute value using SimpleXML for PHP [duplicate] - php

This question already has answers here:
Accessing #attribute from SimpleXML
(10 answers)
Closed 6 years ago.
I use SimpleXMLElement class for working with xml files in my project.
My question is: how to get an attribute value of some tag with some attribute? You may assume I know the name of the tag, the name of the attribute and it's location inside the xml file. For example, for such a string <someTag cp="c2"> knowing values 'someTag' and 'cp' I want to obtain the string "c2".
Thanks is advance.

You can use the attributes() function on the node to get it's attributes:
$xml_str = '<xml>
<node>
<someTag cp="c2">content</someTag>
</node>
</xml>';
$res = simplexml_load_string($xml_str);
$items = $res->xpath("//someTag");
var_dump((string) $items[0]->attributes()->cp);
The returned element is an SimpleXMLElement, so in order to use it I converted it to string (using the (string) cast).

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.

How to remove all xml children with a given element name in PHP? [duplicate]

This question already has answers here:
A simple program to CRUD node and node values of xml file [closed]
(2 answers)
PHP XML remove element and all children by name
(2 answers)
Closed 9 years ago.
How to remove all xml children with a given element name in PHP with SimpleXML?
<xml>
<computer></computer>
<book></book>
<book></book>
</xml>
I need a quick way to remove all "book" elements. Thanks!
select all nodes with xpath and unset them:
$xml = simplexml_load_string($x); // assume XML in $x
$books = $xml->xpath("//book");
foreach ($books as $book) unset($book[0]);
see it in action: https://eval.in/105257

How to remove the element in xml using php depending on their node values [duplicate]

This question already has answers here:
Remove a child with a specific attribute, in SimpleXML for PHP
(18 answers)
Closed 8 years ago.
<?xml version="1.0"?>
<rights>
<visitor>
<user>updateUsers</user>
<user>deleteUsers</user>
<user>createUsers</user>
<user>readUsers</user>
</visitor>
</rights>
This is the xml file i am using. I want to delete the nodes depending on their values. Eg if the user want me to remove the entry for updateUsers then i could remove the <user> that has value updateUsers.
I want to do it using php.
here is my script
$xmlobj=simplexml_load_file($xmlpath);
foreach($xmlobj->{$owner}[0]->children() as $mychild)
{
$childvalues[]=$mychild;
}
foreach($childvalues as $values)
{
if($userentry==$values)
{
//code to remove that node
}
}
I am struck with it. Help will be very much appreciated
Remove the child element from the parent node.
PHP Simple XML remove all Children

How to Convert Xml output as String to Key Value pair in PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have this output:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<disc>SVD1679354</disc>
<cut>18349570</cut>
<previewaac>http://api.7digital.com/1.2/track/preview?trackid=18349570&oauth_consumer_key=7dadeprwudk7&country=GB</previewaac>
<previewmp3>http://api.7digital.com/1.2/track/preview?trackid=18349570&oauth_consumer_key=7dadeprwudk7&country=GB</previewmp3>
<downloadlink></downloadlink>
<codec></codec>
</root>
I want this string output to be read in key value pair so that I can directly read a particular key to get its value.
The output is stored in string but the string represents an xml.
use simplexml to access XML:
$xml = simplexml_load_string($x); // assume XML in $x
echo $xml->cut[0]; // access first <cut> node directly
more on this: http://www.php.net/manual/en/book.simplexml.php
of course, you can transform XML into an array with these functions, too

How do you exclude the XML prolog from output? [duplicate]

This question already has answers here:
PHP DomDocument output without <?xml version="1.0" encoding="UTF-8"?>
(5 answers)
Closed 9 years ago.
I write $xml = new DOMDocument(); and it automatically creates <?xml version="1.0"?>. I need to NOT create it. How do i do that?
One solution is to search the first ">" and strsub at the index at the first < found. But i like a nicer way to do this.
When you saveXML, pass in the root element as the node argument. Only the root element and its contents will be serialised, and not any XML declaration, doctype, comments or PIs outside the root.
$doc->saveXML($doc->documentElement);
or, if you need the other stuff but just not the redundant declaration:
$result= '';
foreach($document->childNodes as $node)
$result.= $document->saveXML($node)."\n";

Categories