Using insertBefore method in php - php

I have actually asked this before, but alas the PC got nicked that I had the solution on, and I no longer can get the previous solution to work.
I'm trying to add a new element to the XML below:
<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
<comments>
<comment>
<date>20120509</date>
<time>10:21:05</time>
<name>Lucy</name>
<text>Hello etc</text>
</comment>
<comment> ...etc
The PHP code I'm using is:
$xml = new DOMDocument('1.0', 'utf-8');
$xml->load(filename.xml);
$parent = $xml->firstChild;
$refnode = $parent->firstChild;
$new = $parent->insertBefore($xml->createElement('comment'), $refnode);
However, this adds a new "comment" immediately after the "data" tag, and if I try to add children (such as "date", "time" etc...) with $new->addChild(tag, value), I get an "undefined method" error. I've tried all manner of permutations, but nothing works.
desired result would be:
<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
<comments>
*<comment>
<date>20140225</date>
<time>17:39:05</time>
<name>Derek</name>
<text>New comment text</text>
</comment>*
<comment>
<date>20120509</date>
<time>10:21:05</time>
<name>Lucy</name>
<text>Hello etc</text>
</comment>
<comment> ...etc

Your XML file most likely contained the whitespace characters like in your example. These are interpreted as text nodes, which renders firstChild useless to obtain one of those elements you want.
You have to iterate over the children instead and get the first one, which is actually a DOMElement. Also you had to go one level deeper than you did. See appended sourcecode which outputs the result you want.
<?php
$xml = new DOMDocument();
$xml->loadXML('<?xml version="1.0" encoding="UTF-8"?>
<data>
<comments>
<comment>
<date>20120509</date>
<time>10:21:05</time>
<name>Lucy</name>
<text>Hello etc</text>
</comment>
</comments>
</data>');
$parent = $xml->firstChild;
foreach ($parent->childNodes as $c) {
if ($c instanceof DOMElement) {
$refnode = $c;
break;
}
}
foreach ($refnode->childNodes as $c) {
if ($c instanceof DOMElement) {
$refnode2 = $c;
break;
}
}
$insert = $xml->createElement('comment', 'test');
$refnode->insertBefore($insert, $refnode2);
echo $xml->saveHTML();

Related

How can I append new xml nodes to an existing .xml file with php class DOMDocument?

Let's say I have the following .xml file:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name>Foo</name>
</item>
<item>
<name>Bar</name>
</item>
</root>
In this sample file, I'm trying to append new nodes <item> to node <root> after the last node <item>.
I'm trying to append newly created <item> nodes after the last <item> node in the <root> node in the .xml file.
<?php
$file = new DOMDocument;
$file->load("xml.xml");
$file->loadXML($file->saveXML());
$root = $file->getElementsByTagName('root')->item(0);
foreach (["Foo_1", "Bar_2", "Foo_3", "Bar_4"] as $val) {
$item = new DOMElement('item');
$item->appendChild(new DOMElement('name', $val));
$root->appendChild(item);
}
?>
But I'm getting an error:
Fatal error: Uncaught Error: Call to a member function appendChild() on null in C:\Users\pfort\Desktop\p.php:12
Stack trace:
#0 {main}
thrown in C:\Users\user_acer\Desktop\p.php on line 12
What am I doing wrong?
There's multiple issues with your example code. I will address the error you received first:
There is no element <terminy> in your example XML, so
$root = $file->getElementsByTagName('terminy')->item(0);
will return null. That's why you are receiving the
Call to a member function appendChild() on null
error at
$root->appendChild(item);
Also, item is a typo, because it's not a valid variable name (but a name for a non-existent constant); you meant $item.
I'm assuming "terminy" means something similar to "root" in your native language and that you actually meant to write
$root = $file->getElementsByTagName('root')->item(0);
By the way: if you want a reference to the root node of an XML document, you can also use $file->docomentElement.
However, there are other issues with your example code:
$file->load("xml.xml");
$file->loadXML($file->saveXML()); // why are you reloading it in this way?
The last line is unnecessary. You are reloading the same XML again. Is it for formatting purposes? If so, there's a better option available:
$file->preserveWhiteSpace = false;
$file->formatOutput = true;
$file->load("xml.xml");
Lastly: you cannot append children to a node that has not been associated with a document yet. So, to create a new item and associate it with the document, you either do (recommended):
// automatically associate new nodes with document
$item = $file->createElement('item');
$item->appendChild($file->createElement('name', $val));
or (more cumbersome):
// import nodes to associate them with document
$item = $file->importNode(new DOMElement('item'));
$item->appendChild($file->importNode(new DOMElement('name', $val)));
So, putting it all together it becomes:
<?php
$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name>Foo</name>
</item>
<item>
<name>Bar</name>
</item>
</root>
XML;
$file = new DOMDocument;
$file->preserveWhiteSpace = false;
$file->formatOutput = true;
$file->loadXML($xml); // (for demo purpose loading above XML) replace this with $file->load("xml.xml"); in your actual code
$root = $file->documentElement;
foreach (["Foo_1", "Bar_2", "Foo_3", "Bar_4"] as $val) {
$item = $file->createElement('item');
$item->appendChild($file->createElement('name', $val));
$root->appendChild($item);
}
echo $file->saveXML();
**PROBLEM SOLVED**
I lost too much time on this problem. The good news is, I already know how to get what I need. Here I offer a solution - for everyone who will need to solve the same problem.
Perhaps this solution will be useful for anyone who needs it.
<?php
// snippet of xml temple
$xml = <<<XML
<item date="%s" status="%s">
<name>%s</name>
</item>
XML;
// prepare snippet
$xmlSnippet = sprintf($xml, "2022-11-21", 0, "Foo Bar");
// new DOMDocument
$dom = new DOMDocument;
$dom->preserveWhiteSpace = 0;
$dom->formatOutput = 1;
// load of .xml file content and load to DOMDocument object
$file = simplexml_load_file("xml.xml");
$dom->loadXML($file->asXML());
// creating of fragment from snippet
$fragment = $dom->createDocumentFragment();
$fragment->appendXML($xmlSnippet);
//append the snippet to the DOMDocument
// and save it to the xml.xml file
$dom->documentElement->appendChild($fragment);
$dom->save("xml.xml");
?>
Result:

PHP - Remove leading and trailing spaces from XML tags

How to remove the leading and trailing white space between open and closing XML?
$sampleXML = '<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<PersonName>
<GivenName> David </GivenName>
<MiddleName> Raj</MiddleName>
<Affix>JR</Affix>
</PersonName>
<Aliases>
<PersonName>
<GivenName></GivenName>
<MiddleName></MiddleName>
<FamilyName></FamilyName>
</PersonName>
</Aliases>
<DemographicDetail>
<GovernmentId countryCode="US">testIDs data </GovernmentId>
<DateOfBirth>2000-12-12</DateOfBirth>
</DemographicDetail>
</note>
<anothertag>
<data type="credit">
<Vendor score="yes"> vendor name </Vendor>
</data>
</anothertag>';
$doc = new DOMDocument;
$doc->loadXML($xml);
foreach ($doc->documentElement->childNodes as $node) {
}
$xpath = new DOMXpath($doc);
$xml = $doc->saveXML($doc, LIBXML_NOEMPTYTAG);
I have tried using getElementsByTagName. But the tag name is dynamic. So it doesn't work for me in this case.
Is their any bulid in php class?
Expected XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<PersonName>
<GivenName>David</GivenName>
<MiddleName>Raj</MiddleName>
<Affix>JR</Affix>
</PersonName>
<Aliases>
<PersonName>
<GivenName></GivenName>
<MiddleName></MiddleName>
<FamilyName></FamilyName>
</PersonName>
</Aliases>
<DemographicDetail>
<GovernmentId countryCode="US">testIDs data</GovernmentId>
<DateOfBirth>2000-12-12</DateOfBirth>
</DemographicDetail>
</note>
<anothertag>
<data type="credit">
<Vendor score="yes">vendor name</Vendor>
</data>
</anothertag>
Thanks in advance.
You can traverse on all the nodes and trim the node value, for this you need a recursive function to traverse all the nodes:
function trimNodes(DOMNode $node) {
foreach ($node->childNodes as $child){
if($child->hasChildNodes()) {
trimNodes($child);
} else{
$child->nodeValue = trim($child->nodeValue);
}
}
}
call this function and send the $doc to it, then you will have your expected XML.
Please note your XML needs to have a root node. You have two root nodes currently (anothertag, note), wrap them in one root node.
If $sampleXML is just a string, then I think a regular expression could give you what you are looking for:
$pattern = '/(?<=\>)\s+(?=[a-zA-Z0-9,\.\_])|(?<=[a-zA-Z0-9,\.\_])\s+(?=\<)/';
$sampleXML = preg_replace($pattern, '', $sampleXML);

Set the namespace for an XML tag with PHP

I'd like to create an XML document with a very specific format. It should look similar to this:
<?xml version="1.0" encoding="UTF-8"?>
<ram:FLOW xmlns:ram=\"http://MY_LIBRARY\" xmlns:mar=\"http://ANOTHER_LIBRARY\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
<Header>
<Source>Application1</Source>
<Time>2014-11-12T12:46:39</Time>
<Environment>TEST</Environment>
<Sequence>537</Sequence>
</Header>
<Data>
<mar:OC_DC>
<DC_elements>
<Unit>
<Unit_ID>089789</Unit_ID>
<State>active</State>
</Unit>
<Unit>
<Unit_ID>459008</Unit_ID>
<State>inactive</State>
</Unit>
</DC_elements>
</mar:OC_DC>
</Data>
</ram:FLOW>
I wrote a PHP/MySQL script to generate this document:
<?php
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ram:FLOW xmlns:ram=\"http://MY_LIBRARY\" xmlns:mar=\"http://ANOTHER_LIBRARY\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></ram:FLOW>");
$header = $xml->addChild('Header');
$header->addChild('Source', $source);
$header->addChild('Time', $time);
$header->addChild('Environment', $env);
$header->addChild('Sequence', $sequence);
$data=$xml->addChild('Data');
$mar_oc_dc=$data->addChild('mar:OC_DC');
$dc_elements=$mar_oc_dc->addChild('DC_elements');
while($condition)
{
// some MySQL code here to extract unit_id and state
$unit=$dc_elements->addChild('Unit');
$unit_id=$unit->addChild('Unit_ID', $unit_id);
$state=$unit->addChild('State', $state);
}
$dom = new DOMDocument();
$dom->preserveWhiteSpace = FALSE;
$dom->formatOutput = TRUE;
$dom->loadXML($xml->asXML());
$handle = fopen("backup/" . $file_name . ".xml", "w");
fwrite($handle, $dom->saveXML());
fclose($handle);
?>
But the result was a little bit different from what I expected:
<?xml version="1.0" encoding="UTF-8"?>
<FLOW xmlns:ram=\"http://MY_LIBRARY\" xmlns:mar=\"http://ANOTHER_LIBRARY\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
<Header>
<Source>Application1</Source>
<Time>2014-11-12T12:46:39</Time>
<Environment>TEST</Environment>
<Sequence>537</Sequence>
</Header>
<Data>
<OC_DC>
<DC_elements>
<Unit>
<Unit_ID>089789</Unit_ID>
<State>active</State>
</Unit>
<Unit>
<Unit_ID>459008</Unit_ID>
<State>inactive</State>
</Unit>
</DC_elements>
</OC_DC>
</Data>
</FLOW>
As you can see, the ram:FLOW tag became FLOW, and the mar:OC_DC tag became OC_DC.
I looked on Stack Overflow and other websites for a solution and didn't manage to find one. Could you please give me a hand with this?
Thank you in advance.
The xmlns:* attributes are namespace definitions (not libraries). The value of that attributes is a unique string that identifies the format/standard the elements belong to.
The attributes define a prefix for the unique string so that the XML document is smaller and more readable.
If you want to create an element (or attribute) inside a namespace you have to provide the namespace. In SimpleXMlElement the third argument is the namespace.
It seems to add the elements to the namespace of the parent node, if no namespace is provided. That means that you have to provide an empty string for any element without a namespace.
$root = new SimpleXMlElement('<ram:FLOW xmlns:ram="http://MY_LIBRARY" xmlns:mar="http://ANOTHER_LIBRARY"/>');
$root->addChild('header', null, '');
$data = $root->addChild('data', null, '');
$data->addChild('mar:OC_DC', null, 'http://ANOTHER_LIBRARY');
echo $root->asXml();
Output:
<?xml version="1.0"?>
<ram:FLOW xmlns:ram="http://MY_LIBRARY" xmlns:mar="http://ANOTHER_LIBRARY">
<header xmlns=""/>
<data xmlns="">
<mar:OC_DC/>
</data>
</ram:FLOW>
I haven't found a way to avoid the empty xmlns attributes.
DOM is more explicit. The create and append logic is separate.
const XMLNS_RAM = 'http://MY_LIBRARY';
const XMLNS_MAR = 'http://ANOTHER_LIBRARY';
$dom = new DOMDocument();
// appending an element with a namespace with define it if needed
$root = $dom->appendChild($dom->createElementNS(XMLNS_RAM, 'ram:FLOW'));
// setting the xmlns attribute explicit avoids the definition in descendant nodes
$root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:mar', XMLNS_MAR);
$root->appendChild($dom->createElement('header'));
$data = $root->appendChild($dom->createElement('data'));
$data->appendChild($dom->createElementNS(XMLNS_MAR, 'mar:OC_DC'));
$dom->formatOutput = true;
echo $dom->saveXml();
Output:
<?xml version="1.0"?>
<ram:FLOW xmlns:ram="http://MY_LIBRARY" xmlns:mar="http://ANOTHER_LIBRARY">
<header/>
<data>
<mar:OC_DC/>
</data>
</ram:FLOW>

Hide XML declaration in files generated using PHP

I was tesing with a simple example of how to display XML in browser using PHP and found this example which works good
<?php
$xml = new DOMDocument("1.0");
$root = $xml->createElement("data");
$xml->appendChild($root);
$id = $xml->createElement("id");
$idText = $xml->createTextNode('1');
$id->appendChild($idText);
$title = $xml->createElement("title");
$titleText = $xml->createTextNode('Valid');
$title->appendChild($titleText);
$book = $xml->createElement("book");
$book->appendChild($id);
$book->appendChild($title);
$root->appendChild($book);
$xml->formatOutput = true;
echo "<xmp>". $xml->saveXML() ."</xmp>";
$xml->save("mybooks.xml") or die("Error");
?>
It produces the following output:
<?xml version="1.0"?>
<data>
<book>
<id>1</id>
<title>Valid</title>
</book>
</data>
Now I have got two questions regarding how the output should look like.
The first line in the xml file '', should not be displayed, that is it should be hidden
How can I display the TextNode in the next line. In total I am exepecting an output in this fashion
<data>
<book>
<id>1</id>
<title>
Valid
</title>
</book>
</data>
Is that possible to get the desired output, if so how can I accomplish that.
Thanks
To skip the XML declaration you can use the result of saveXML on the root node:
$xml_content = $xml->saveXML($root);
file_put_contents("mybooks.xml", $xml_content) or die("cannot save XML");
Please note that saveXML(node) has a different output from saveXML().
First question:
here is my post where all usable threads with answers are listed: How do you exclude the XML prolog from output?
Second question:
I don't know of any PHP function that outputs text nodes like that.
You could:
read xml using DomDocument and save each node as string
iterate trough nodes
detect text nodes and add new lines to xml string manually
At the end you would have the same XML with text node values in new line:
<node>
some text data
</node>

Retrieving a subset of XML nodes with PHP

Using PHP, how do I get an entire subset of nodes from an XML document? I can retrieve something like:
<?xml version="1.0" encoding="utf-8"?>
<people>
<certain>
<name>Jane Doe</name>
<age>21</age>
</certain>
<certain>
<certain>
<name>John Smith</name>
<age>34</age>
</certain>
</people>
But what if I only want to return the child nodes of like this?
<certain>
<name>Jane Doe</name>
<age>21</age>
</certain>
<certain>
<certain>
<name>John Smith</name>
<age>34</age>
</certain>
EDIT: I'm trying to get a subset of XML and pass that directly, not an object like simplexml would give me. I am basically trying to get PHP to do what .NET's OuterXml does... return literally the above subset of XML as is... no interpreting or converting or creating a new XML file or anything... just extract those nodes in situ and pass them on. Am I going to have to get the XML file, parse out what I need and then rebuild it as a new XML file? If so then I need to get rid of the <?xml version="1.0" encoding="utf-8"?> bit... ugh.
The answer would be to use XPath.
$people = simplexml_load_string(
'<?xml version="1.0" encoding="utf-8"?>
<people>
<certain>
<name>Jane Doe</name>
<age>21</age>
</certain>
<certain>
<name>John Smith</name>
<age>34</age>
</certain>
</people>'
);
// get all <certain/> nodes
$people->xpath('//certain');
// get all <certain/> nodes whose <name/> is "John Smith"
print_r($people->xpath('//certain[name = "John Smith"]'));
// get all <certain/> nodes whose <age/> child's value is greater than 21
print_r($people->xpath('//certain[age > 21]'));
Take 2
So apparently you want to copy some nodes from a document into another document? SimpleXML doesn't support that. DOM has methods for that but they're kind of annoying to use. Which one are you using? Here's what I use: SimpleDOM. In fact, it's really SimpleXML augmented with DOM's methods.
include 'SimpleDOM.php';
$results = simpledom_load_string('<results/>');
foreach ($people->xpath('//certain') as $certain)
{
$results->appendChild($certain);
}
That routine finds all <certain/> node via XPath, then appends them to the new document.
You could use DOMDocument.GetElementsByTagName or you could:
Use XPath?
<?php
$xml = simplexml_load_file("test.xml");
$result = $xml->xpath("//certain");
print_r($result);
?>
Use DOM and XPath. Xpath allows you to select nodes (and values) from an XML DOM.
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
$result = '';
foreach ($xpath->evaluate('/people/certain') as $node) {
$result .= $dom->saveXml($node);
}
echo $result;
Demo: https://eval.in/162149
DOMDocument::saveXml() has a context argument. If provided it saves that node as XML. Much like outerXml(). PHP is able to register your own classes for the DOM nodes, too. So it is even possible to add an outerXML() function to element nodes.
class MyDomElement extends DOMElement {
public function outerXml() {
return $this->ownerDocument->saveXml($this);
}
}
class MyDomDocument extends DOMDocument {
public function __construct($version = '1.0', $encoding = 'utf-8') {
parent::__construct($version, $encoding);
$this->registerNodeClass('DOMElement', 'MyDomElement');
}
}
$dom = new MyDomDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
$result = '';
foreach ($xpath->evaluate('/people/certain') as $node) {
$result .= $node->outerXml();
}
echo $result;
Demo: https://eval.in/162157
See http://www.php.net/manual/en/domdocument.getelementsbytagname.php
The answer turned out to be a combination of the xpath suggestion and outputting with asXML().
Using the example given by Josh Davis:
$people = simplexml_load_string(
<?xml version="1.0" encoding="utf-8"?>
<people>
<certain>
<name>Jane Doe</name>
<age>21</age>
</certain>
<certain>
<name>John Smith</name>
<age>34</age>
</certain>
</people>'
);
// get all <certain/> nodes
$nodes = $people->xpath('/people/certain');
foreach ( $nodes as $node ) {
$result .= $node->asXML()."\n";
}
echo $result;

Categories