I can store normal string. But if I tried to store GET method url it can not store.
function updateX_xml($id,$val,$addre){
$xml = new DOMDocument();
$xml->load('autoGen/autoGen.xml');
$node = $xml->getElementsByTagName('root')->item(0) ;
$xml_id = $xml->createElement("id");
$xml_addres = $xml->createElement("Address");
$domAttribute = $xml->createAttribute('type');
$domAttribute->value = 'xs:string';
$xml_addres->appendChild($domAttribute);
$xml_url = $xml->createElement("url");
$xml_id->nodeValue=$id;
$xml_url->nodeValue=$val;
$xml_addres->nodeValue=$addre;
$node->appendChild( $xml_id );
$node->appendChild( $xml_url );
$xml->formatOutput = true;
$xml->save("autoGen/autoGen.xml");
}
if i call this function like this updateX_xml(1,'getdata?event_id=1 &lan=en',"addaress"); it is not working.
This will generate this warning. Warning: updateX_xml(): unterminated entity reference lan=en in C:\xampp\htdocs\test_file_read\gen_url.php on line 25
If you are inserting something into XML/HTML you should always use the htmlspecialchars function. this will escape your strings into correct XML syntax.
So:
function updateX_xml($id,$val,$addre)
{
$xml = new DOMDocument();
$xml->load('autoGen/autoGen.xml');
$node = $xml->getElementsByTagName('root')->item(0) ;
$xml_id = $xml->createElement("id");
$xml_addres = $xml->createElement("Address");
$domAttribute = $xml->createAttribute('type');
$domAttribute->value = 'xs:string';
$xml_addres->appendChild($domAttribute);
$xml_url = $xml->createElement("url");
$xml_id->nodeValue=$id;
$xml_url->nodeValue = htmlspecialchars($val);
$xml_addres->nodeValue=$addre;
$node->appendChild( $xml_id );
$node->appendChild( $xml_url );
$xml->formatOutput = true;
$xml->save("autoGen/autoGen.xml");
}
You have to escape HTML entity character:
$val= htmlentities($str, ENT_XML1);
$xml_url->nodeValue=$val;
Try without space between parameters:
updateX_xml(1,'getdata?event_id=1&lan=en',"addaress");
Also, as others mentioned, you need to escape the "&", as it's a special character in xml using htmlspecialchars() :
$xml_url->nodeValue = htmlspecialchars($val);
Related
I'm trying to extract all meta http-equiv properties from url.
Here is the code
function fetch_http_equiv($url)
{
$data = file_get_contents($url);
$dom = new DomDocument;
#$dom->loadHTML($data);
$xpath = new DOMXPath($dom);
$metas = $xpath->query('//*/meta[starts-with(#http-equiv)]');
$http_equiv = array();
foreach($metas as $meta){
$property = $meta->getAttribute('http-equiv');
$content = $meta->getAttribute('content');
$http_equiv[$property] = $content;
}
return $http_equiv;
}
// fetch meta http-equiv 's
$http_equiv = fetch_http_equiv($link);
// if $http_equiv Content-Language exists
if (empty($http_equiv['Content-Language'])) {
}else{
$meta_content_language = $http_equiv['Content-Language'];
}
For the love of god In my mind it should work, what did I missed ?
edit:
I found a problem; I did changed
$property = $meta->getAttribute('http_equiv');
to
$property = $meta->getAttribute('http-equiv');
case solved.
I found a problem; I did changed
$property = $meta->getAttribute('http_equiv');
to
$property = $meta->getAttribute('http-equiv');
case solved.
Code works now.
The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.
dom create error like above,, and page return blank when I create new element in meta and data node like this :
<?php
$this->module->daftarkanJs('underscore-min.js');
$form = CJSON::decode(file_get_contents(Yii::app()->getBaseUrl(true).'/index.php/odk/api/index/id/'.$_GET['id']));
$input = CJSON::decode(file_get_contents(Yii::app()->getBaseUrl(true).'/index.php/odk/api/input/id/'.$_GET['id']));
function haveChild($id, $input_id){
$child = CJSON::decode(file_get_contents(Yii::app()->getBaseUrl(true).'/index.php/odk/api/child/id/'.$id.'/parentId/'.$input_id));
if($child['result']){ // jika child ada
foreach($child['result'] as $data){
// echo '- <b>'.$data['input_id'].'</b><br/>';
haveChild($id, $data['input_id']);
}
return true;
}else{
return false;
}
}
function loop($inputResult, $id){
$dom = new DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$html = $dom->createElementNS('http://www.w3.org/2002/xforms', 'h:html');
$html->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:h', 'http://www.w3.org/1999/xhtml');
$html->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:ev', 'http://www.w3.org/2001/xml-events');
$html->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
$html->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:jr', 'http://openrosa.org/javarosa');
$html = $dom->appendChild($html);
$head = $dom->createElement('h:h');
$head = $html->appendChild($head);
$title = $dom->createElement('h:t', 'xxxxxxx');
$title = $head->appendChild($title);
$model = $dom->createElement('m');
$model = $head->appendChild($model);
$instance = $dom->createElement('instance');
$instance = $model->appendChild($instance);
$data = $dom->createElement('data');
$data = $instance->appendChild($data);
$meta = $dom->createElement('meta');
$meta = $data->appendChild($meta);
$instanceID = $dom->createElement('instaceID');
$instanceID = $meta->appendChild($instanceID);
$bind = $dom->createElement('bind');
$bind->setAttribute("nodeset","/data/meta/instanceID");
$bind = $model->appendChild($bind);
foreach($inputResult as $data){
if(!$data['parent_id']){ // ambil yang bukan child
$check = haveChild($id, $data['input_id']);
if(!$check){
$data = $dom->createElement('data');
$data = $instance->appendChild($data);
$meta = $dom->createElement('meta');
$meta = $data->appendChild($meta);
$bind = $dom->createElement('bind');
$bind->setAttribute("nodeset","/data/".str_replace(" ", "_", $data['name']));
$bind = $model->appendChild($bind);
}
}
}
$body = $dom->createElement('h:b');
$body = $html->appendChild($body);
printf ("<pre>%s</pre>", htmlentities ($dom->saveXML()));
}
loop($input['result'], $_GET['id']);
?>
error in this line :
$data = $dom->createElement('data');
$data = $instance->appendChild($data);
$meta = $dom->createElement('meta');
$meta = $data->appendChild($meta);
You don't output the XML, but HTML with escaped XML.
<pre>some escaped xml</pre>
This output matches the error message if it is treated as XML. Here is no XML declaration with an encoding.
Stripped down to the DOM methods, your source outputs an XML document: https://eval.in/private/1507ef8a4065d0.
However, I suggest to use createElementNS() for ALL namespaced nodes. Calls like $dom->createElement('h:h'); are ambiguous.
$xmlns = [ 'h' => 'http://www.w3.org/1999/xhtml' ];
$dom = new DOMDocument();
$html = $dom->appendChild(
$dom->createElementNS($xmlns['h'], 'h:html')
);
$head = $html->appendChild($dom->createElementNS($xmlns['h'], 'h:head'));
echo $dom->saveXml();
Output:
<?xml version="1.0"?>
<h:html xmlns:h="http://www.w3.org/1999/xhtml"><h:head/></h:html>
Assume $html_dom contains a page that has HTML entities like . In the output below, I get an output like this  .
$html_dom = new DOMDocument();
#$html_dom->loadHTML($html_doc);
$xpath = new DOMXPath($html_dom);
$query = '//div[#class="foo"]/div/p';
$my_foos = $xpath->query($query_abstract);
foreach ($my_foos as $my_foo)
{
echo html_entity_decode($my_foos->nodeValue);
die;
}
How do I handle this properly so that I don't get weird characters? I tried the following with no success:
$html_doc = mb_convert_encoding($html_doc, 'HTML-ENTITIES', 'UTF-8');
$html_dom = new DOMDocument();
$html_dom->resolveExternals = TRUE;
#$html_dom->loadHTML($html_doc);
$xpath = new DOMXPath($html_dom);
$query = '//div[#class="foo"]/div/p';
$my_foos = $xpath->query($query);
foreach ($my_foos as $my_foo)
{
echo html_entity_decode($my_foos->nodeValue);
die;
}
mb_convert_encoding was a good idea, but it does not work as expected because DOMDocument seems to be a little big buggy when it comes to encoding.
Moving the mb_convert_encoding to the actual node output did the trick.
$html_dom = new DOMDocument();
$html_dom->resolveExternals = TRUE;
#$html_dom->loadHTML($html_doc);
$xpath = new DOMXPath($html_dom);
$query = '//div[#class="foo"]/div/p';
$my_foos = $xpath->query($query);
foreach ($my_foos as $my_foo)
{
echo mb_convert_encoding($my_foo->nodeValue, 'HTML-ENTITIES', 'UTF-8');
die;
}
I try to get some attiributue values. But have no chance. Below yo can see my code and explanation. How to get duration, file etc.. values?
$url="http://www.some-url.ltd";
$dom = new DOMDocument;
#$dom->loadHTMLFile($url);
$xpath = new DOMXPath($dom);
$the_div = $xpath->query('//div[#id="the_id"]');
foreach ($the_div as $rval) {
$the_value = trim($rval->getAttribute('title'));
echo $the_value;
}
The output below:
{title:'title',
description:'description',
scale:'fit',keywords:'',
file:'http://xxx.ccc.net/ht/2012/05/10/419EE45F98CD63F88F52CE6260B9E85E_c.mp4',
type:'flv',
duration:'24',
screenshot:'http://xxx.ccc.net/video/2012/05/10/419EE45F98CD63F88F52CE6260B9E85E.jpg?v=1336662169',
suggestion_path:'/videoxml/player_xml/61319',
showSuggestions:true,
autoStart:true,
width:412,
height:340,
autoscreenshot:true,
showEmbedCode:true,
category: 1,
showLogo:true
}
How to get duration, file etc.. values?
What about
$parsed = json_decode($the_value, true);
$duration = $parsed['duration'];
EDIT:
Since json_decode() requires proper JSON formatting (key names and values must be enclosed in double quotes), we should fix original formatting into the correct one. So here is the code:
function my_json_decode($s, $associative = false) {
$s = str_replace(array('"', "'", 'http://'), array('\"', '"', 'http//'), $s);
$s = preg_replace('/(\w+):/i', '"\1":', $s);
$s = str_replace('http//', 'http://', $s);
return json_decode($s, $associative);
}
$parsed = my_json_decode($var, true);
Function my_json_decode is taken from this answer, slightly modified.
I have an irritating problem using PHP's DOMdocument. I have loaded HTML, and changed some of the element's attributes. I want to save the changed HTML, and output it.
The strange thing is, when I use ->saveHTML() or ->saveXML() my closing tags' slashes become escaped. I could remove the escaping with regex, but I would like to know if there is any cleaner way...
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML ($roosterHTML);
$dom->preserveWhiteSpace = false;
libxml_clear_errors();
libxml_use_internal_errors(false);
$tables = $dom->getElementsByTagName('table');
$cols = $tables->item(0)->getElementsByTagName('td');
$name = preg_replace("/(\\n|\\r| )/", "", $cols->item(3)->nodeValue);
$sirname = preg_replace("/(\\n|\\r| )/", "", $cols->item(2)->nodeValue);
$class = preg_replace("/(\\n|\\r| )/", "", $cols->item(1)->nodeValue);
$header = "Rooster van $name $sirname ($class)";
$rooster = $tables->item(1);
$firstRow = true;
foreach ($rooster->getElementsByTagName('tr') as $row) {
if ($firstRow) {
$firstRow = false;
continue;
}
$firstCol = true;
foreach ($row->getElementsByTagName('td') as $col) {
if ($firstCol) {
$firstCol = false;
continue;
}
$text = $col->nodeValue;
$col->setAttribute('style','background-color:#FF0');
//$return.= $text;
}
}
$rooster = $dom->saveXML($rooster);
Testing (just click submit, to send a POST value):
http://bit.ly/ymK3DA
No, the escaped is caused by the json
which mean this page is not output HTML but json-alike plain text