PHP Saving XML and Arrays - php

I have an xml file that is created thru php. The basic structure is:
<catgories> *this is root*
<category> *element*
<title>xy</title> *attribute*
<desc>yu</desc> *attribute*
<categoryLeaf> *child element of category*
<title>rt</title> *attribute of categoryLeaf*
</categoryLeaf>
<endtvod></endtvod>
</category>
</categories>
The xml is created on the fly at one time with the exception of the categoryLeaf and it's attributes which are created in a separate function. The xml is created correctly. In order to get the categoryLeaf I am parsing an html and extracting information and storing that information in an array with the follwoing:
if (!empty($dom)) { //IF NOT EMPTY FIND CATEGORIES
$clsort = $xpath->query('//div[#class="sort"]');
foreach($clsort as $clsorts){
$li= $clsorts->getElementsByTagName('li');
foreach ($li as $lis){
$links=$lis->getElementsByTagname('a');
foreach ($links as $link){
$href=$link->getAttribute('href');
$text=$link->nodeValue;
if ($text=="#"){
break;
}else{
$textarray=$text;
ECHO $textarray."<br>";
CategoryLeafXml($textarray);
}
}
}
}
}
else
{
echo "EMPTY CONTAINER". "<br>";
}
The desired information is obtained thru the code above and added to the textarray. I can echo it and each shows up. I pass the array to a function that is to open the xml, insert the categoryLeaf along with the title attribute and save thru code:
Function CategoryLeafXml($textarray){
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$xpath = new DOMXPath($xml);
$xml-> loadXML ('.\\xml\\test.xml');
$arrcount=count($textarray);
echo $arrcount."<br>";
$categoryLeaf=$xml->createElement("categoryLeaf");
for ($i=0; $i<$arrcount;$i++){
echo $textarray."<br>";
$endtvod=$xml->getElementsByTagName('endtvod')->item(0); //LOCATE tag
$category=$xml->getElementsByTagName('category');
$categories=$xml->getElementsByTagName('categories');
$newleaf=$xml->insertBefore($categoryLeaf, $endtvod);
$title = $xml->createAttribute("title");
$title->value= $textarray;
$categoryLeaf->appendChild($title);
$xml->save('.\\xml\\test.xml');
}
}
What is suppose to occur is the xml structure is created, the html is parse into the textarray, the textarray is passed to the function where the element categoryLeaf is inserted before the element tvod. The textarray is the attribute value for the categoryLeaf/title. I am having 2 problems, (1)the xml is opened and the categoryLeaf is created, but when saved the result is:
<?xml version="1.0" encoding="UTF-8"?>
<categoryLeaf title="value of textarray"/>
The xml is overwritten leaving only the one inserted element with only the last array value.
(2) The array count always shows 1 and the for loop only runs one time writing only the array's last value. I anticipated a categoryLeaf being added for value of the array count but it is only seeing that value as being one. I know there are about 25 entries in the array.

You are only creating a single categoryLeaf node through:
$categoryLeaf=$xml->createElement("categoryLeaf")
and changing the attribute n times.
You have to create the categoryLeaf inside the for loop.
I also do not understand why you are doing this:
$category=$xml->getElementsByTagName('category');
$categories=$xml->getElementsByTagName('categories');
That does not seem to do anything inside the function.
Just try something like this:
Function CategoryLeafXml($textarray){
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$xpath = new DOMXPath($xml);
$xml-> loadXML ('.\\xml\\test.xml');
$endtvod = $xml->getElementsbyTagName('endtvod')->item(0)
foreach ($textarray as $text){
$categoryLeaf=$xml->createElement("categoryLeaf");
$categoryLeaf->setAttribute('title', $text);
$endtvod->parentNode->insertBefore($categoryLeaf, $enttvod)
}
$xml->save('.\\xml\\test.xml');
}

Related

Replace element value in DOM

I want to save DOM tags value to exist XML, I found replace function but it is in js and I need the function in PHP
I tried save and saveXML function, but this didn't worked. I have tags in XML with colon "iaiext:auction_title". I used getElement and it's work good, next i cut title to 50 characters function work too, but how i can replace old title to this new title if i dont use path like simple_load_file. How to show in my script this path?
$dom = new DOMDocument;
$dom->load('p.xml');
$i = 0;
$tytuly = $dom->getElementsByTagName('auction_title');
foreach ($tytuly as $tytul){
$title = $tytul->nodeValue;
$end_title = doTitleCut($title);
//echo "<pre>";
//echo($end_title);
//echo "<pre>";
$i = $i+1;
}
In your loop, you can update a particular nodes value the same way you fetch it - with nodeValue. So in your loop, just update it each time...
$tytul->nodeValue = doTitleCut($title);
Then after your loop, you can just echo the new XML out using
echo $dom->saveXML();
or save it using
$dom->save("3.xml");
It is the same basic API in PHP. However browsers implement more or other parts of the API. Here are 5 revisions of the API (DOM Level 1 to 4 and DOM LS). DOM 3 added a property to read/write the text content of a node: https://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-textContent
The following example prefixes the titles:
$xml = <<<'XML'
<auctions>
<auction_title>World!</auction_title>
<auction_title>World & Universe!</auction_title>
</auctions>
XML;
$document = new DOMDocument();
$document->loadXML($xml);
$titleNodes = $document->getElementsByTagName('auction_title');
foreach ($titleNodes as $titleNode) {
$title = $titleNode->textContent;
$titleNode->textContent = 'Hello '.$title;
}
echo $document->saveXML();
Output:
<?xml version="1.0"?>
<auctions>
<auction_title>Hello World!</auction_title>
<auction_title>Hello World & Universe!</auction_title>
</auctions>
PHPs DOMNode::$nodeValue implementation does not match the W3C API definition. It behaves the same as DOMNode::$textContent for reads and does not fully escape on write.

Parsing inline tags with SimpleXML

I'm using SimpleXML & PHP to parse an XML element in the following form:
<element>
random text with <inlinetag src="http://url.com/">inline</inlinetag> XML to parse
</element>
I know I can reach inlinetag using $element->inlinetag, but I don't know how to reach it in such a way that I can basically replace the inlinetag with a link to the attribute source without using it's location in the text. The result would basically have to look like this:
here is a random text with inline XML
This may be a stupid questions, I hope someone here can help! :)
I found a way to do this using DOMElement.
One way to replace the element is by cloning it with a different name/attributes. Here is is a way to do this, using the accepted answer given on How do you rename a tag in SimpleXML through a DOM object?
function clonishNode(DOMNode $oldNode, $newName, $replaceAttrs = [])
{
$newNode = $oldNode->ownerDocument->createElement($newName);
foreach ($oldNode->attributes as $attr)
{
if (isset($replaceAttrs[$attr->name]))
$newNode->setAttribute($replaceAttrs[$attr->name], $attr->value);
else
$newNode->appendChild($attr->cloneNode());
}
foreach ($oldNode->childNodes as $child)
$newNode->appendChild($child->cloneNode(true));
$oldNode->parentNode->replaceChild($newNode, $oldNode);
}
Now, we use this function to clone the inline element with a new element and attribute name. Here comes the tricky part: iterating over all the nodes will not work as expected. The length of the selected nodes will change as you clone them, as the original node is removed. Therefore, we only select the first element until there are no elements left to clone.
$xml = '<element>
random text with <inlinetag src="http://url.com/">inline</inlinetag> XML to parse
</element>';
$dom = new DOMDocument;
$dom->loadXML($xml);
$nodes= $dom->getElementsByTagName('inlinetag');
echo $dom->saveXML(); //<element>random text with <inlinetag src="http://url.com/">inline</inlinetag> XML to parse</element>
while($nodes->length > 0) {
clonishNode($nodes->item(0), 'a', ['src' => 'href']);
}
echo $dom->saveXML(); //<element>random text with inline XML to parse</element>
That's it! All that's left to do is getting the content of the element tag.
Is this the result you want to achieve?
<?php
$data = '<element>
random text with
<inlinetag src="http://url.com/">inline
</inlinetag> XML to parse
</element>';
$xml = simplexml_load_string($data);
foreach($xml->inlinetag as $resource)
{
echo 'Your SRC attribute = '. $resource->attributes()->src; // e.g. name, price, symbol
}
?>

Change the value of a text node using SimpleXML

I am trying to write a code where it will find a specific element in my XML file and then change the value of the text node. The XML file has different namespaces. Till now, I have managed to register the namespaces and also echo the text node of the element, which I want to change.
<?php
$xml = simplexml_load_file('getobs.xml');
$xml->registerXPathNamespace('g','http://www.opengis.net/gml');
$result = $xml->xpath('//g:beginPosition');
foreach ($result as $title) {
echo $title . "\n";
}
?>
My question is: How can I change the value of this element using SimpleXML? I tried to use the nodeValue command but I am not able to make it work.
This is a part of the XML:
<sos:GetObservation xmlns:sos="http://www.opengis.net/sos/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" service="SOS" version="1.0.0" srsName="urn:ogc:def:crs:EPSG:4326">
<sos:offering>urn:gfz:cawa:def:offering:meteorology</sos:offering>
<sos:eventTime>
<ogc:TM_During xmlns:ogc="http://www.opengis.net/ogc" xsi:type="ogc:BinaryTemporalOpType">
<ogc:PropertyName>urn:ogc:data:time:iso8601</ogc:PropertyName>
<gml:TimePeriod xmlns:gml="http://www.opengis.net/gml">
<gml:beginPosition>2011-02-10T01:10:00.000</gml:beginPosition>
Thanks
Dimitris
In the end I managed to do it by using the PHP XML DOM.
Here is the code that I used in order to change the text node of a specific element:
<?php
// create new DOM document and load the data
$dom = new DOMDocument;
$dom->load('getobs.xml');
//var_dump($dom);
// Create new xpath and register the namespace
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('g','http://www.opengis.net/gml');
// query the result amd change the value to the new date
$result = $xpath->query("//g:beginPosition");
$result->item(0)->nodeValue = 'sds';
// save the values in a new xml
file_put_contents('test.xml',$dom->saveXML());
?>
Not wanting to switch from the code I've already made for SimpleXML, I found this solution:
http://www.dotdragnet.com/forum/index.php?topic=3979.0
Specificially:
$numvotes = $xml->xpath('/gallery/image[path="'.$_GET["image"].'"]/numvotes');
...
$numvotes[0][0] = $votes;
Hope this helps!

How can i append some elements to a particular element in xml using php?

I am trying to append some data in XML file using PHP.
I am searching for a word if it is already present in XML or not.
If it's present i only have to append the data to it otherwise i have to create whole element.
For this i am using DOMXPath. Code is as follows:
$fname="ontology.xml";
$xml=new DomDocument;
$xml->Load($fname);
$xpath = new DOMXPath($xml);
$query = '//RelatedTerms/words/word/word_title[.="'.$word.'"]';
$entries=$xpath->query($query);
if($entries->length!=0)
{
foreach($entries as $entry)
{
$wordElement=$xml->getElementsByTagName($entry->parentNode->nodeName)->item(0);
//Problem is here even if the <word> is found at the second position it append the element to the first <word> element.
$newIsaElement=$xml->createElement('isa');
$newIsaTitleElement=$xml->createElement('isa_title',"sample");
$newRelevanceTitle=$xml->createElement('relevance',"9");
$newIsaElement->appendChild($newIsaTitleElement);
$newIsaElement->appendChild($newRelevanceTitle);
//$newIsaTitleElement->appendChild($newIsaElement);
$wordElement->appendChild($newIsaElement);
$xml->save($fname);
}
}
else
{
echo "In Here!";
$words=$xml->getElementsByTagName('words')->item(0);
$newWordElement=$xml->createElement('word');
$newWordTitleElement=$xml->createElement('word_title',$word);
$newIsaElement=$xml->createElement('isa');
$newIsaTitleElement=$xml->createElement('isa_title',"test");
$newRelevanceTitle=$xml->createElement('relevance',"2");
$newWordElement->appendChild($newWordTitleElement);
$newIsaElement->appendChild($newIsaTitleElement);
$newIsaElement->appendChild($newRelevanceTitle);
$newWordElement->appendChild($newIsaElement);
$words->appendChild($newWordElement);
$xml->save($fname);
}
I want to add the element to the same element in which the word is found. Please help me through this!
Thanks!
PS:Here is a format of xml file if u need!
<RelatedTerms>
<words>
<word>
<word_title>algo</word_title>
<isa>
<isa_title>algorithm</isa_title>
<relevance>9</relevance>
</isa>
<hasa>
<hasa_title>Algorithmic Example</hasa_title>
<relevance>7</relevance>
</hasa>
<akindof>
<akindof_title>Pseudo Code</akindof_title>
<relevance>8</relevance>
</akindof>
<partof>
<partof_title>Data Structures</partof_title>
<relevance>9</relevance>
</partof>
</word>
<word>
<word_title>algo</word_title>
<isa>
<isa_title>test</isa_title>
<relevance>9</relevance> //instead of adding it here it adds to the above element
</isa>
</word>
</words>
$entries is not an array, it is a DOMNodeList object. Check if empty with $entries->length == 0.

How to get a specific node text using php DOM

I am trying to get the value (text) of a specific node from an xml document using php DOM classes but I cannot do it right because I get the text content of that node merged with its descendants.
Let's suppose that I need to get the trees from this document:
<?xml version="1.0"?>
<trees>
LarchRedwoodChestnutBirch
<trimmed>Larch</trimmed>
<trimmed>Redwood</trimmed>
</trees>
And I get:
LarchRedwoodChestnutBirchLarchRedwood
You can see that I cannot remove the substring LarchRedwood made by the trimmed trees from the whole text because I would get only ChestnutBirch and it is not what I need.
Any suggest? (Thanx)
I got it. This works:
function specificNodeValue($node, $implode = true) {
$value = array();
if ($node->childNodes) {
for ($i = 0; $i < $node->childNodes->length; $i++) {
if (!(#$node->childNodes->item($i)->tagName)) {
$value[] = $node->childNodes->item($i)->nodeValue;
}
}
}
return (is_string($implode) ? implode($implode, $value) : ($implode === true ? implode($value) : $value));
}
A given node is like a root, if you get no tagName when you parse its child nodes then it is itself, so the value of that child node it is its own value.
Inside a bad formed xml document a node could have many pieces of value, put them all into an array to get the whole value of the node.
Use the function above to get needed node value without subnode values merged within.
Parameters are:
$node (required) must be a DOMElement object
$implode (optional) if you want to get a string (true by default) or an array (false) made up by many pieces of value. (Set a string instead of a boolean value if you wish to implode the array using a "glue" string).
You can try this to remove the trimmed node
$doc = new DOMDocument('1.0', 'utf-8');
$doc->loadXML($xml);
$xpath = new DOMXpath($doc);
$trees = $doc->getElementsByTagName('trees')->item(0);
foreach ($xpath->query('/trees/*') as $node)
{
$trees->removeChild($node);
}
echo $trees->textContent;
echo $trees->nodeValue;
Use $node->nodeValue to get a node's text content. If you use $node->textContent, you get all text from the current node and all child nodes.
Ideally, the XML should be:
<?xml version="1.0"?>
<trees>
<tree>Larch</tree>
<tree>Redwood</tree>
<tree>Chestnut</tree>
<tree>Birch</tree>
</trees>
To split "LarchRedwoodChestnutBirch" into separate words (by capital letter), you'll need to use PHP's "PCRE" functions:
http://www.php.net/manual/en/book.pcre.php
'Hope that helps!

Categories