Remove a xml node in php - php

my xml file:
<temporary>
<users>
<temp>
<id>1</id>
<title> Undercover</title>
<author>Wiwit</author>
</temp>
<nissi>
<confirm>3977678bce8515e8cdbfa64850904ad1</confirm>
<firstname>hi</firstname>
<lastname>hhey</lastname>
<day>1</day>
</nissi>
</users>
</temporary>
my php:
<?php
$user="nissi";
$xml = simplexml_load_file("temporary.xml")
or die("Error: Cannot create object");
unset($xml->temporary->users->$user);
?>
Why is this not working.
The unset is not working.The node ins'nt getting deleted.

It works like this :
$user="nissi";
$xml = simplexml_load_file("temporary.xml")
or die("Error: Cannot create object");
unset($xml->users->$user);
echo $xml->asXML();
You mustn't take the root of your xml in the "request" temporary here.
DEMO HERE

You cannot do it with SimpleXML alone, you have to use DOMElement conversion as explained here:
Remove a child with a specific attribute, in SimpleXML for PHP

Related

How to parse xml output

I want to get the content text of an xml output. How do I parse this in php? Please see code below.
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[this is a test]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>
Try this :
$x = '<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[this is a test]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>';
$xml = simplexml_load_string($x) or die("Error: Cannot create object");
print_r($xml);

Adding or Updating XML Node PHP? [duplicate]

I just wanted to ask how I can insert a new node in an XML using PHP. my XML file (questions.xml) is given below
<?xml version="1.0" encoding="UTF-8"?>
<Quiz>
<topic text="Preparation for Exam">
<subtopic text="Science" />
<subtopic text="Maths" />
<subtopic text="english" />
</topic>
</Quiz>
I want to add a new "subtopic" with the "text" attribute, that is "geography". How can I do this using PHP? Thanks in advance though.
well my code is
<?php
$xmldoc = new DOMDocument();
$xmldoc->load('questions.xml');
$root = $xmldoc->firstChild;
$newElement = $xmldoc->createElement('subtopic');
$root->appendChild($newElement);
// $newText = $xmldoc->createTextNode('geology');
// $newElement->appendChild($newText);
$xmldoc->save('questions.xml');
?>
I'd use SimpleXML for this. It would look somehow like this:
// Open and parse the XML file
$xml = simplexml_load_file("questions.xml");
// Create a child in the first topic node
$child = $xml->topic[0]->addChild("subtopic");
// Add the text attribute
$child->addAttribute("text", "geography");
You can either display the new XML code with echo or store it in a file.
// Display the new XML code
echo $xml->asXML();
// Store new XML code in questions.xml
$xml->asXML("questions.xml");
The best and safe way is to load your XML document into a PHP DOMDocument object, and then go to your desired node, add a child, and finally save the new version of the XML into a file.
Take a look at the documentation : DOMDocument
Example of code:
// open and load a XML file
$dom = new DomDocument();
$dom->load('your_file.xml');
// Apply some modification
$specificNode = $dom->getElementsByTagName('node_to_catch');
$newSubTopic = $xmldoc->createElement('subtopic');
$newSubTopicText = $xmldoc->createTextNode('geography');
$newSubTopic->appendChild($newSubTopicText);
$specificNode->appendChild($newSubTopic);
// Save the new version of the file
$dom->save('your_file_v2.xml');
You can use PHP's Simple XML. You have to read the file content, add the node with Simple XML and write the content back.

How can I get a particular node from SimpleXML

My XML is the following: http://api.napiarfolyam.hu/?valuta=eur
I would like to get only the 'vetel' value where the 'bank' is mnb.
My PHP is the following:
<?php
$xml=simplexml_load_file("http://api.napiarfolyam.hu/?valuta=eur") or die("Error: Cannot create object");
$vetel = (string) $xml->valuta->bank->vetel;
?>
This one should be the solution but the problem is, according to your xml file, bank mnb has no vetel. So the iterator can't see bank mnb.
$xml=simplexml_load_file("http://api.napiarfolyam.hu/?valuta=eur") or die("Error: Cannot create object");
foreach($xml->valuta->item as $item){
if($item->bank == "mnb"){
echo $item->vetel;
break;
}
}
As has already been said, there's no such node in the XML you've provided. The only <item> where <bank> is equal to "mnb" is as follows, without a <vetel> tag.
<item>
<bank>mnb</bank>
<datum>2016-09-07 11:25:32</datum>
<penznem>EUR</penznem>
<kozep>309.3500</kozep>
</item>
Using the kozep tag as an example though, you can easily use SimpleXML's xpath() method to find the matching value, e.g.
$xml = file_get_contents('http://api.napiarfolyam.hu/?valuta=eur');
$sxml = simplexml_load_string($xml);
echo (float) $sxml->xpath('/arfolyamok/deviza/item[bank = "mnb"]/kozep/text()')[0];
// 309.35

Simple xml returns no values when trying to access nodes

Hi guys very new to the php world.
I am listening for a PHP post that contains xml, when the xml is retrieved i need to access individual nodes. I am able to echo the full xml file but not individual attributes.
Currently I am just sending the data using a chrome extension Postman. There is no front end code. Here is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<job>
<job_ref>abc123</job_ref>
<job_title>Test Engineer</job_title>
</job>
And here is my PHP:
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$xml = file_get_contents('php://input');
echo $xml;
$xml=simplexml_load_file($xml);
echo $xml->job_ref . "<br>";
echo $xml->job_title . "<br>";
}else{
die();
}
Any hep wopuld be amazing am I am very stuck.
Many thanks
simplexml_load_file expect PATH to the XML file, not its content. You have to use simplexml_load_string instead:
$xml = simplexml_load_string($xml);

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>

Categories