php SimpleXMLElement set text - php

how to set text for a SimpleXMLElement in php?

Let's say $node is an SimpleXMLElement. This code:
$node->{0} = "some string"
will result in an extra childNode in PHP 7, instead of setting the text content for $node. Use:
$node[0] = "some string"
instead.

Did you look at the basic documentation examples?
From there:
include 'example.php';
$xml = new SimpleXMLElement($xmlstr);
$xml->movie[0]->characters->character[0]->name = 'Miss Coder';
echo $xml->asXML();

$xml = SimpleXMLElement('<a><b><c></c></b></a>');
$foundNodes = $xml->xpath('//c');
$foundNode = $foundNodes[0];
$foundNode->{0} = "This text will be put inside of c tag.";
$xml->asXML();
// will output <a><b><c>This text will be put inside of c tag.</c></b></a>
More on my search of this answer here:
How can I set text value of SimpleXmlElement without using its parent?

Related

<![CDATA[ ]]> returns empty description simplexml_load_file() php [duplicate]

I noticed that when using SimpleXMLElement on a document that contains those CDATA tags, the content is always NULL. How do I fix this?
Also, sorry for spamming about XML here. I have been trying to get an XML based script to work for several hours now...
<content><![CDATA[Hello, world!]]></content>
I tried the first hit on Google if you search for "SimpleXMLElement cdata", but that didn't work.
You're probably not accessing it correctly. You can output it directly or cast it as a string. (in this example, the casting is superfluous, as echo automatically does it anyway)
$content = simplexml_load_string(
'<content><![CDATA[Hello, world!]]></content>'
);
echo (string) $content;
// or with parent element:
$foo = simplexml_load_string(
'<foo><content><![CDATA[Hello, world!]]></content></foo>'
);
echo (string) $foo->content;
You might have better luck with LIBXML_NOCDATA:
$content = simplexml_load_string(
'<content><![CDATA[Hello, world!]]></content>'
, null
, LIBXML_NOCDATA
);
The LIBXML_NOCDATA is optional third parameter of simplexml_load_file() function. This returns the XML object with all the CDATA data converted into strings.
$xml = simplexml_load_file($this->filename, 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>";
print_r($xml);
echo "</pre>";
Fix CDATA in SimpleXML
This did the trick for me:
echo trim($entry->title);
This is working perfect for me.
$content = simplexml_load_string(
$raw_xml
, null
, LIBXML_NOCDATA
);
When to use LIBXML_NOCDATA ?
I add the issue when transforming XML to JSON.
$xml = simplexml_load_string("<foo><content><![CDATA[Hello, world!]]></content></foo>");
echo json_encode($xml, true);
/* prints
{
"content": {}
}
*/
When accessing the SimpleXMLElement object, It gets the CDATA :
$xml = simplexml_load_string("<foo><content><![CDATA[Hello, world!]]></content></foo>");
echo $xml->content;
/* prints
Hello, world!
*/
I makes sense to use LIBXML_NOCDATA because json_encode don't access the SimpleXMLElement to trigger the string casting feature, I'm guessing a __toString() equivalent.
$xml = simplexml_load_string("<foo><content><![CDATA[Hello, world!]]></content></foo>", null, LIBXML_NOCDATA);
echo json_encode($xml);
/*
{
"content": "Hello, world!"
}
*/
While using SimpleXMLElement class directly
new SimpleXMLElement($rawXml, LIBXML_NOCDATA);

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.

What's wrong with this attempt to use SimpleXML?

I can't seem to get anything to work with simpleXML for PHP. What is wrong with the following:
$xml = simplexml_load_string('<book><title>The Title</title></book>');
$title = $xml->book->title;
echo "<pre>title = $title\n</pre>";
The resulting output is:
title =
Why isn't the output as follows?
title = The Title
Please advise.
Since <book> is the root node of this snippet, you need $xml->title rather than $xml->book->title.
$xml = simplexml_load_string('<book><title>The Title</title></book>');
$title = $xml->title;
echo "<pre>title = $title\n</pre>";
// Prints
<pre>title = The Title
</pre>
The structure is more easily discovered if you var_dump() it:
var_dump($xml);
object(SimpleXMLElement)#1 (1) {
["title"]=>
string(9) "The Title"
}
Try
$str = '<book><title>The Title</title></book>';
$xml = new SimpleXMLElement($str);
$title = $xml->book->title;
echo $title;
What I suspect the problem being is that you haven't created the XML object and are trying to use a method from that object. Thats my assumption given your code snippet.
Take a look at PHP: Simple XML

How to append source html to a DOMElement in PHP?

Is there a way of appending source html into a DOMElement? Something like this:
$trElement->appendSource("<a href='?select_user=4'>Username</a>");
It would parse that fragment and then append it.
You are looking for
- DOMDocumentFragment::appendXML — Append raw XML data
Example from Manual:
$doc = new DOMDocument();
$doc->loadXML("<root/>");
$f = $doc->createDocumentFragment();
$f->appendXML("<foo>text</foo><bar>text2</bar>");
$doc->documentElement->appendChild($f);
echo $doc->saveXML();
If you don't have a reference to the document root in scope, you can always access it via the ownerDocument property of an arbitrary node:
$frag = $trElement->ownerDocument->createDocumentFragment();
$frag->appendXML("<a href='?select_user=4'>Username</a>");
$trElement->appendChild($frag);
Yes, you can do this with DOMDocument::createDocumentFragment:
$fragment = $dom->createDocumentFragment();
$fragment->appendXML('Username');
$element->appendChild($fragment);
In this case, it would be simpler to do it with a normal createElement call:
$el = $dom->createElement('a', 'Username');
$el->setAttribute('href', 'select_user=4');
$element->appendChild($el);
In each case, $element is the DOM element to which you want to append your code.

PHP: How to handle <![CDATA[ with SimpleXMLElement?

I noticed that when using SimpleXMLElement on a document that contains those CDATA tags, the content is always NULL. How do I fix this?
Also, sorry for spamming about XML here. I have been trying to get an XML based script to work for several hours now...
<content><![CDATA[Hello, world!]]></content>
I tried the first hit on Google if you search for "SimpleXMLElement cdata", but that didn't work.
You're probably not accessing it correctly. You can output it directly or cast it as a string. (in this example, the casting is superfluous, as echo automatically does it anyway)
$content = simplexml_load_string(
'<content><![CDATA[Hello, world!]]></content>'
);
echo (string) $content;
// or with parent element:
$foo = simplexml_load_string(
'<foo><content><![CDATA[Hello, world!]]></content></foo>'
);
echo (string) $foo->content;
You might have better luck with LIBXML_NOCDATA:
$content = simplexml_load_string(
'<content><![CDATA[Hello, world!]]></content>'
, null
, LIBXML_NOCDATA
);
The LIBXML_NOCDATA is optional third parameter of simplexml_load_file() function. This returns the XML object with all the CDATA data converted into strings.
$xml = simplexml_load_file($this->filename, 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>";
print_r($xml);
echo "</pre>";
Fix CDATA in SimpleXML
This did the trick for me:
echo trim($entry->title);
This is working perfect for me.
$content = simplexml_load_string(
$raw_xml
, null
, LIBXML_NOCDATA
);
When to use LIBXML_NOCDATA ?
I add the issue when transforming XML to JSON.
$xml = simplexml_load_string("<foo><content><![CDATA[Hello, world!]]></content></foo>");
echo json_encode($xml, true);
/* prints
{
"content": {}
}
*/
When accessing the SimpleXMLElement object, It gets the CDATA :
$xml = simplexml_load_string("<foo><content><![CDATA[Hello, world!]]></content></foo>");
echo $xml->content;
/* prints
Hello, world!
*/
I makes sense to use LIBXML_NOCDATA because json_encode don't access the SimpleXMLElement to trigger the string casting feature, I'm guessing a __toString() equivalent.
$xml = simplexml_load_string("<foo><content><![CDATA[Hello, world!]]></content></foo>", null, LIBXML_NOCDATA);
echo json_encode($xml);
/*
{
"content": "Hello, world!"
}
*/
While using SimpleXMLElement class directly
new SimpleXMLElement($rawXml, LIBXML_NOCDATA);

Categories