How to add an attribute to XML in PHP? [duplicate] - php

This question already has answers here:
Unable to add namespace to an attribute with PHP's SimpleXML
(3 answers)
Closed 8 years ago.
I am very new to XML so apologies if this is a simple question;
I have the following line of code in a PHP file which is used to create a resulting line of code in an XML file via a PHP form;
$Savings = $xml->createElement('Savings');
This creates the following line in my XML file;
<Savings>263.4</Savings>
How would I change this line in my XML file to look as follows;
<Savings Currency="EUR">263.4</Savings>
I have tried the following but I get no additional output;
$Savings = $xml->createElementNS('EUR', 'Savings');

Use setAttribute:
$Savings = $xml->createElement('Savings');
$Savings->setAttribute('Currency', 'EUR');

Related

php xml. remove only comment <!-- keep tags and values [duplicate]

This question already has answers here:
Commenting and Un-Commenting a Node in an XML Document
(1 answer)
How to retrieve comments from within an XML Document in PHP
(4 answers)
Closed 1 year ago.
have a xml file that i read with php. In there is a commented section that i wanna read like it wasnt a comment, like it was real in the xml structure. For example:
$xml_str = "<test><tag></tag></test> <!-- <report><tag1>test</tag1><tag2>test2</tag2></report> -->";
How can i do this, that i remove the tag from report? With
foreach ($xpath->query('//comment()') as $comment)
{
var_dump($comment->textContent);
}
i have only the text strings but not the tags and so on. Uncommenting it when reading the file would be great

How to write to specific line of the file using PHP? [duplicate]

This question already has answers here:
PHP what is the best way to write data to middle of file without rewriting file
(3 answers)
How to insert some text in middle of file in PHP [closed]
(4 answers)
Closed 4 years ago.
I am generating some PHP source code files using PHP only :)
And I want to know if I have to write some line of text before closing brackets, how can I do that using PHP file functions.
e.g. below is my target file code. filename : project.php
Class project{
public function get_project($id){
..... some code here
}
public function add_project(){
..... some code here
}
<--- add code here
}
I have a line of code which I want to add to the position pointed in the example. I can use file_put_content() method but it has option to Append the code on the end of the file like below
file_put_contents('project.php', 'Hello World', FILE_APPEND);
but it will not write to specific position. So how can I do that?

Can't use method return value in write context PHP unset() [duplicate]

This question already has answers here:
Remove a child with a specific attribute, in SimpleXML for PHP
(18 answers)
Closed 7 years ago.
I know there are a lot of answers out there for this exact question, but none of them seem to help me solve my problem.
I have an xml file on my server, i need to use PHP SimpleXML to remove an element from the document. After some googling i found a number of answers saying to use unset() and then save the xml.
so i came up with this:
function deleteCourse($course){
$xml = self::getxml(); # get the XML file
unset($xml->xpath('course[name = "'.$course.'"]'));
$xml->asXml("data.xml");
}
now whenever i run this i get this error: PHP Fatal error: Can't use method return value in write context in blahblahLink on line 92
line 92 is unset($xml->xpath('course[name = "'.$course.'"]'));
I really hope somebody can help me out with this
unset won't work if you pass method return, pass variable content / array instead

How to read in XML strings as variables? [duplicate]

This question already has answers here:
A simple program to CRUD node and node values of xml file [closed]
(2 answers)
Closed 8 years ago.
Try this query:
http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da
How do I simply read in the suggestions as variables? I can't seem to find anything that explains this.
You can use SimpleXmlIterator. That's really easy to use and you will be able to perform a foreach on the object you will get.
Library source
For example with file_get_contents or replace with curl if you prefer:
$feed = new SimpleXmlIterator(file_get_contents('http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da'));
foreach ($feed->suggestion as $suggestion) {
echo $value;
}

How to detect new line from the parsed XML with simpleload_xml_string? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP SimpleXML doesn't preserve line breaks in XML attributes
I have following XML
$xmldatas = '<layer text="name
id"></layer>';
I have parse this XML with
$xml = simplexml_load_string($xmldatas);
But when I checked the $xml, the \n is been replaced with space. I want the new line remains as it is after the xml parsing.
But how can I do that ?
Thanks
I don't think that xml will accept a new line character in the tag option text.
If you are generating the xml maybe you want to do something like this?
$xmldatas = '<layer><text>name
id</text></layer>';

Categories