Get value from a CDATA in xml - php

I dont know how get a lat, lon in php values from this xml code:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<name>OpenCellID Cells</name>
<description>List of available cells</description>
<Placemark><name></name><description><![CDATA[lat: <b>3.378199</b><br/>lon: <b>-76.523528</b><br/>mcc: <b>732</b><br/>mnc: <b>123</b><br/>lac: <b>4003</b><br/>cellid: <b>26249364</b><br/>averageSignalStrength: <b>0</b><br/>samples: <b>10</b><br/>changeable: <b>1</b>]]></description><Point><coordinates>-76.523528,3.378199,0</coordinates></Point></Placemark>
</Document>
</kml>
I hope you can help me with this. Thanks

The trick is to read out the cdata as a string first, let libxml wrap it into welformatted html and then parse out the values from the nodes containing your data.
Note that this works but assumes that lon and lat are always in the first nodes in the cdata
// the xml as a variable
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<name>OpenCellID Cells</name>
<description>List of available cells</description>
<Placemark><name></name><description><![CDATA[lat: <b>3.378199</b><br/>lon: <b>-76.523528</b><br/>mcc: <b>732</b><br/>mnc: <b>123</b><br/>lac: <b>4003</b><br/>cellid: <b>26249364</b><br/>averageSignalStrength: <b>0</b><br/>samples: <b>10</b><br/>changeable: <b>1</b>]]></description><Point><coordinates>-76.523528,3.378199,0</coordinates></Point></Placemark>
</Document>
</kml>';
// read into dom
$domdoc = new DOMDocument();
$domdoc->loadXML($xml);
// the cdata as a string
$cdata = $docdom->getElementsByTagName('Placemark')->item(0)->getElementsByTagName('description')->item(0)->nodeValue;
// a dom object for the cdata
$htmldom = new DOMDocument();
// wrap in html and parse
$htmldom->loadHTML($cdata);
// get the <b> nodes
$bnodes = $htmldom->getElementsByTagName('b');
// your data :)
$lon = $bnodes->item(0)->nodeValue;
$lat = $bnodes->item(1)->nodeValue;
Last not least, this is to illustrate how loadXML and loadHTML differ and how to use that. As for googleeart kml, I am sure that is a more standard way to parse ...

Related

Convert array to XML with both tags on empty value [duplicate]

I want to generate xml by using php simplexml.
$xml = new SimpleXMLElement('<xml/>');
$output = $xml->addChild('child1');
$output->addChild('child2', "value");
$output->addChild('noValue', '');
Header('Content-type: text/xml');
print($xml->asXML());
The output is
<xml>
<child1>
<child2>value</child2>
<noValue/>
</child1>
</xml>
What I want is if the tag has no value it should display like this
<noValue></noValue>
I've tried using LIBXML_NOEMPTYTAG from Turn OFF self-closing tags in SimpleXML for PHP?
I've tried $xml = new SimpleXMLElement('<xml/>', LIBXML_NOEMPTYTAG); and it doesn't work. So I don't know where to put the LIBXML_NOEMPTYTAG
LIBXML_NOEMPTYTAG does not work with simplexml, per the spec:
This option is currently just available in the DOMDocument::save and DOMDocument::saveXML functions.
To achieve what you're after, you need to convert the simplexml object to a DOMDocument object:
$xml = new SimpleXMLElement('<xml/>');
$child1 = $xml->addChild('child1');
$child1->addChild('child2', "value");
$child1->addChild('noValue', '');
$dom_sxe = dom_import_simplexml($xml); // Returns a DomElement object
$dom_output = new DOMDocument('1.0');
$dom_output->formatOutput = true;
$dom_sxe = $dom_output->importNode($dom_sxe, true);
$dom_sxe = $dom_output->appendChild($dom_sxe);
echo $dom_output->saveXML($dom_output, LIBXML_NOEMPTYTAG);
which returns:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<child1>
<child2>value</child2>
<noValue></noValue>
</child1>
</xml>
Something worth pointing out... the likely reason that the NOEMPTYTAG option is available for DOMDocument and not simplexml is that empty elements are not considered valid XML, while the DOM specification allows for them. You are banging your head against the wall to get invalid XML, which may suggest that the valid self-closing empty element would work just as well.
Despite the quite lengthy answers already given - which are not particularly wrong and do shed some light into some libxml library internals and it's PHP binding - you're most likely looking for:
$output->noValue = '';
To add an open tag, empty node-value and end tag (beautified, demo is here: http://3v4l.org/S2PKc):
<?xml version="1.0"?>
<xml>
<child1>
<child2>value</child2>
<noValue></noValue>
</child1>
</xml>
Just noting as it seems it has been overlooked with the existing answers.
Since Simple XML is proving troublesome, perhaps XMLWriter could do what you want.
Here's a fiddle
<?php
$oXMLWriter = new XMLWriter;
$oXMLWriter->openMemory();
$oXMLWriter->startDocument('1.0', 'UTF-8');
$oXMLWriter->startElement('xml');
$oXMLWriter->writeElement('child1', 'Hello world!!');
$oXMLWriter->writeElement('noValue', '');
$oXMLWriter->endElement();
$oXMLWriter->endDocument();
echo htmlentities($oXMLWriter->outputMemory(TRUE));
?>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<child1>Hello world!!</child1>
<noValue></noValue>
</xml>
Extending from my comments (some got deleted):
The behavior depends on your environment. This same code:
$xml = new SimpleXMLElement('<xml/>', LIBXML_NOEMPTYTAG);
$output = $xml->addChild('child1');
$output->addChild('child2', "value");
$output->addChild('noValue', '');
echo $xml->asXML();
In 3v4l.org and Ideone.com, it produces self-closing tag;
In eval.in, codepad.org and on my localhost (PHP 5.3/5.4, libXML 2.7, Windows), it produces empty tag.
Since according to PHP document, LIBXML_NOEMPTYTAG only (guarenteed to) work in DOM, you may want to try DOM for ensurance:
$dom=new DOMDocument("1.0","UTF-8");
$dom->formatOutput=true;
$root=$dom->createElement("xml");
$dom->appendChild($root);
$child1=$dom->createElement("child1");
$root->appendChild($child1);
$node=$dom->createElement("child2");
$node->appendChild($dom->createTextNode("value"));
$child1->appendChild($node);
$node=$dom->createElement("noValue");
$child1->appendChild($node);
echo $dom->saveXML($dom,LIBXML_NOEMPTYTAG);
3v4l.org demo.
Edit:
All the above online demo site uses Content-Type: text/plain by default. If you want to directly output XML to browser as a standalone resource, you can specify the header before output:
header("Content-Type: text/xml");
echo $dom->saveXML($dom,LIBXML_NOEMPTYTAG);
I had to get the child node and asign to the nodeValue property an empty string, and the xml close tag appears.
$output->childNodes[1]->nodeValue = '';
Greetings.

How to add an attribute to an XML Element in PHP without the xml header?

I need to add a new attribute to xml elements stored in DB. This is not a complete XML, but just XML elements in the DB.
When I do the following,
$node_xml = simplexml_load_string('<node name="nodeA">this is a node</node>');
$node_xml->addAttribute('newattrib', "attribA");
$res_xml = $node_xml->asXML();
I get $res_xml as:
"<?xml version=\"1.0\"?>\n<node name=\"nodeA\" newattrib=\"attribA\">this is a node</node>\n"
How do I eliminate the <?xml version=\"1.0\"?> part without doing a string manipulation?
Add the root level and then get the node but not the full xml. In that case the header will not be echoed
$string = '<node name="nodeA">this is a node</node>';
$node_xml = simplexml_load_string('<root>'. $string .'</root>');
$node = $node_xml->children()[0];
$node->addAttribute('newattrib', "attribA");
echo $res_xml = $node->asXML(); // <node name="nodeA" newattrib="attribA">this is a node</node>
demo
Here we are using DOMDocument for setting attribute in a tag.
<?php
ini_set('display_errors', 1);
libxml_use_internal_errors(true);
$domObject = new DOMDocument();
$domObject->loadHTML('<node name="nodeA">this is a node</node>',LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD);
$result=$domObject->getElementsByTagName("node")->item(0);
$result->setAttribute("newattrib","attribA");
echo $result->ownerDocument->saveHTML();
Output: <node name="nodeA" newattrib="attribA">this is a node</node>

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>

How to convert an XML string into a DOMDocument in PHP?

I got an example XML string from a customer, which I would like to transform to a DOMDocument. I can't seem to get the first node right though....
The string looks like this;
<ev:Events xmlns:ev="xsdEvents" xsi:schemaLocation="xsdEvents [url]" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Node>
<OtherNode>value</OtherNode>
</Node>
</ev:Events>
How would I set this up the proper way via DOMDocument in PHP?
Well, loading a XML string to a DOMDocument object is not quite that hard -- you'll just have to use DOMDocument::loadXML().
For example, in your case, you'd use :
$string = <<<XML
<ev:Events xmlns:ev="xsdEvents" xsi:schemaLocation="xsdEvents [url]" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Node>
<OtherNode>value</OtherNode>
</Node>
</ev:Events>
XML;
$dom = new DOMDocument();
$dom->loadXML($string);
Then, accessing your data is just a matter of using the relevant DOM methods.
For example, to extract the value of your <OtherNode> node, you could use :
$items = $dom->getElementsByTagName('OtherNode');
if ($items->length > 0) {
var_dump( $items->item(0)->nodeValue );
}

SimpleXML get node value

Say I have this following XML structure:
<?xml version="1.0" encoding="UTF-8"?>
<main>
<parent>
<child1>some value</child1>
<child2>another value</child2>
</parent>
</main>
I made a variable of the XML and now I want to get the values of child1, so I use SimpleXML:
$xml = new SimpleXMLElement($xml);
$this->xmlcode = (string) $xml->main->parent->child1;
But I get this message: Notice: Trying to get property of non-object in /x.php on line x
I also tried it with $xml->parent->child1, but no success.
Anyone??
$xml = new SimpleXMLElement($xml);
$this->xmlcode = (string) $xml->parent[0]->child1;
A good example of using XPath with php for the SimpleXMLElement can be found here
http://www.php.net/manual/en/class.simplexmlelement.php#95229
// Find the topmost element of the domDocument
$xpath = new DOMXPath($xml);
$child1 = $xpath->evaluate('/main/parent/child1')->item(0);
Variant for xpath (Also how to get content of node having dashes in name):
<?xml version="1.0" encoding="UTF-8"?> <main>
<parent>
<child-1>some value</child-1>
<child-2>another value</child-2>
</parent> </main>
$xml = simplexml_load_string($content);
$node_value= (string)$xml->xpath('parent/child-1')[0];
result of $node_value:
"some value"

Categories