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";
Related
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).
This question already has answers here:
PHP SimpleXML get innerXML
(11 answers)
Closed 6 years ago.
I am using PHP SimpleXML to parse data in a file. Say for example I have the following XML content:
<?xml version="1.0" ?>
<root>
<parent1>
<child1>blah</child1>
<child2>blah blah</child2>
</parent1>
<parent2>blah</parent2>
</root>
I basically want to get the actual raw content of the inside of a node. I.e. I need it to return the "innerXML" of parent1 just as text, tags and all. Having trouble getting PHP to do this. Help?
For getting the exact content '<child1>blah</child1><child2>blah blah</child2>' as itself you better put them under <![CDATA[]]> Then you can get any thing inside that as iself.
xml should be like this::
<?xml version="1.0" ?>
<root>
<parent1>
<![CDATA[<child1>blah</child1>
<child2>blah blah</child2>]]>
</parent1>
<parent2>blah</parent2>
</root>
Then
$xml = simplexml_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA);
$parent1 = $xml->parent1->asXml();
echo "<pre>";echo ($parent1);echo "<pre>";
This will output :
<child1>blah</child1>
<child2>blah blah</child2>
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
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
This question already has answers here:
Simple XML reading question (PHP)
(3 answers)
Closed 9 years ago.
I need some php code to read this XML, and take the content of the <title>
tags and declare as a $title variable. And the same for the <tags> and <content> as $tags and $content variables respectively.
The XML is listed below:
<?xml version="1.0" encoding="ISO-8859-1"?>
<page>
<title>Example title</title>
<tags>example,title,page</tags>
<content>This page has some lovely example content</content>
</page>
this has been talked many times on stackoverflow, try to learn about simplexml. you need to load the file and then loop inside
$data = simplexml_load_string($xml);
foreach($data as $key => $val)
echo "$key=>$val" . "<br>";
live working sample -> http://codepad.viper-7.com/6r1Tnd
You need to read loadXML. The link describes this function and gives some example code on how to use it.