I have the following XML code:
<para>
<![CDATA[
<?php
$data = '<?xml version="1.0"?>
<root>content</root>';
$sxe = simplexml_load_string($data);
var_dump($sxe);
?>
]]>
</para>
I want to parse the CDATA section to take this result:
Content:
<?php
$data = '<?xml version="1.0"?>
<root>content</root>';
$sxe = simplexml_load_string($data);
var_dump($sxe);
?>
I use the following but it doesn't work.
$xml='sxml.xml';
$book = simplexml_load_file($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$para = $book->chapter->para[1];
print "Content: ".$para."<br>";
foreach($para AS $node) {
print "Iter Content: ".$node."<br>";
}
This results in:
Content:
content';
$sxe = simplexml_load_string($data);
var_dump($sxe);
?>
In phpinfo() it shows that libxml is enabled and active.
Any help? Thanks in advance
You should encode the special characters using htmlspecialchars
For example:
print "Content: ".htmlspecialchars($para)."<br>";
Related
I tried to remove the attribute "xmlns:xhtml" from the tag "xhtml:link" with the following code:
Source Code:
$doc = new DOMDocument('1.0', 'utf-8');
$url = 'android-app://com.domain.name';
$element = $doc->createElementNS($url,'xhtml:link');
$attribute = $doc->childNodes->item(0);
//echo '<br>tag: '.$doc->getElementsByTagName("xhtml:link")[0];
$element->setAttribute('href', $url);
$element->setAttribute('rel', 'alternate');
//echo '<pre>';print_r($element);echo '</pre>';
$element->hasAttributeNS($url, 'xhtml');
$element->removeAttributeNS($url, 'xhtml');
$doc->appendChild($element);
echo $doc->saveXML();
OutPut:
<?xml version="1.0" encoding="utf-8"?>
<default:link href="android-app://com.domain.name" rel="alternate"/>
But, I am expecting the output looks like:
<?xml version="1.0" encoding="utf-8"?>
<xhtml:link href="android-app://com.domain.name" rel="alternate"/>
Please help me what I have to do? Here I struck to replace the tag...
Thanks!
Try createElement function instead of createElementNS:
$doc = new DOMDocument('1.0', 'utf-8');
$url = 'android-app://com.domain.name';
$element = $doc->createElement('xhtml:link');
$attribute = $doc->childNodes->item(0);
$element->setAttribute('href', $url);
$element->setAttribute('rel', 'alternate');
$doc->appendChild($element);
echo $doc->saveXML();
OUTPUT:
<?xml version="1.0" encoding="utf-8"?>
<xhtml:link href="android-app://com.domain.name" rel="alternate"/>
I want to add an XML tree into another XML, and I have tried with following code which is not working:
<?php
$str1 = '<parent>
<name>mrs smith</name>
</parent>';
$xml1 = simplexml_load_string($str1);
print_r($xml1);
$str2 = '<tag>
<child>child1</child>
<age>3</age>
</tag>';
$xml2 = simplexml_load_string($str2);
print_r($xml2);
$xml1->addChild($xml2);
print_r($xml1);
?>
Expect output XML:
<parent>
<name>mrs smith</name>
<tag>
<child>child1</child>
<age>3</age>
</tag>
</parent>
Please assist me.
You can use DOMDocument::importNode
<?php
$str2 = '<tag>
<child>child1</child>
<age>3</age>
</tag>';
$str1 = '<parent>
<name>mrs smith</name>
</parent>';
$tagDoc = new DOMDocument;
$tagDoc->loadXML($str2);
$tagNode = $tagDoc->getElementsByTagName("tag")->item(0);
//echo $tagDoc->saveXML();
$newdoc = new DOMDocument;
$newdoc->loadXML($str1);
$node = $newdoc->importNode($tagNode, true);
$newdoc->documentElement->appendChild($node);
echo $newdoc->saveXML();die;
According to this answer it is possible to echo out formatted xml. Yet this php code:
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><data></data>");
$xml->addChild("child1", "value1");
$xml->addChild("child2", "value2");
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
echo $dom->saveXML();
outputs value1 value2
So how do I format it correctly nowadays?
To echo out formatted XML (or HTML) you have to use htmlentities built-in function, that “convert all applicable characters to HTML entities”.
In your case:
echo htmlentities($dom->saveXML());
will output this:
<?xml version="1.0" encoding="utf-8"?> <data> <child1>value1</child1> <child2>value2</child2> </data>
Using-it together with <pre> html tag, also newlines and spaces will be printed:
echo '<pre>' . htmlentities($dom->saveXML()) . '</pre>';
will output this:
<?xml version="1.0" encoding="utf-8"?>
<data>
<child1>value1</child1>
<child2>value2</child2>
</data>
I'm trying to deal with some XML in PHP.
I have code, such as this:
<?php
$stream = fopen("xml","r");
?>
Where "xml" contains something such as this:
<name>name1</name>
<key>key1</key>
<name>name2</name>
<key>key2</key>
etc.
I'd like to create an array out of the contents of the <key> tags, something like where
keys[0] = "key1"
and
keys[1] = "key2"
Any help is appreciated, thank you very much :)
Solution:
$xmlstr = fread($stream,filesize("xml-file"));
$sxe = new SimpleXMLElement($xmlstr);
echo $sxe->getName() . "\n";
foreach ($sxe->children() as $child) {
echo $child->children();
}
You should use DOM functions for this case. Let's suppose a well-formed XML document (xmltest.xml):
<?xml version="1.0" encoding="utf-8"?>
<root>
<name>name1</name>
<key>key1</key>
<name>name2</name>
<key>key2</key>
</root>
This code loads the xml file into DOM document and gets all nodes with tag key;
<?php
$dom = new DOMDocument('1.0','utf-8');
$dom->load('xmltest.xml');
$keys = $dom->getElementsByTagName('key');
for ($i = 0; $i < $keys->length; $i++) {
echo $keys->item($i)->nodeValue . "</br>";
}
?>
I would like to create a new simplified xml based on an existing one:
(using "simpleXml")
<?xml version="1.0" encoding="UTF-8"?>
<xls:XLS>
<xls:RouteInstructionsList>
<xls:RouteInstruction>
<xls:Instruction>Start</xls:Instruction>
</xls:RouteInstruction>
</xls:RouteInstructionsList>
<xls:RouteInstructionsList>
<xls:RouteInstruction>
<xls:Instruction>End</xls:Instruction>
</xls:RouteInstruction>
</xls:RouteInstructionsList>
</xls:XLS>
Because there are always colons in the element-tags, it will mess with "simpleXml", I tried to use the following solution->link.
How can I create a new xml with this structure:
<main>
<instruction>Start</instruction>
<instruction>End</instruction>
</main>
the "instruction-element" gets its content from the former "xls:Instruction-element".
Here is the updated code:
But unfortunately it never loops through:
$source = "route.xml";
$xmlstr = file_get_contents($source);
$xml = #simplexml_load_string($xmlstr);
$new_xml = simplexml_load_string('<main/>');
foreach($xml->children() as $child){
print_r("xml_has_childs");
$new_xml->addChild('instruction', $child->RouteInstruction->Instruction);
}
echo $new_xml->asXML();
there is no error-message, if I leave the "#"…
/* the use of # is to suppress warning */
$xml = #simplexml_load_string($YOUR_RSS_XML);
$new_xml = simplexml_load_string('<main/>');
foreach ($xml->children() as $child)
{
$new_xml->addChild('instruction', $child->RouteInstruction->Instruction);
}
/* to print */
echo $new_xml->asXML();
You could use xpath to simplify things. Without knowing the full details, I don't know if it will work in all cases:
$source = "route.xml";
$xmlstr = file_get_contents($source);
$xml = #simplexml_load_string($xmlstr);
$new_xml = simplexml_load_string('<main/>');
foreach ($xml->xpath('//Instruction') as $instr) {
$new_xml->addChild('instruction', (string) $instr);
}
echo $new_xml->asXML();
Output:
<?xml version="1.0"?>
<main><instruction>Start</instruction><instruction>End</instruction></main>
Edit: The file at http://www.gps.alaingroeneweg.com/route.xml is not the same as the XML you have in your question. You need to use a namespace like:
$xml = #simplexml_load_string(file_get_contents('http://www.gps.alaingroeneweg.com/route.xml'));
$xml->registerXPathNamespace('xls', 'http://www.opengis.net/xls'); // probably not needed
$new_xml = simplexml_load_string('<main/>');
foreach ($xml->xpath('//xls:Instruction') as $instr) {
$new_xml->addChild('instruction', (string) $instr);
}
echo $new_xml->asXML();
Output:
<?xml version="1.0"?>
<main><instruction>Start (Southeast) auf Sihlquai</instruction><instruction>Fahre rechts</instruction><instruction>Fahre halb links - Ziel erreicht!</instruction></main>