I am trying to create XML using DOMDocument from database table. All field types are showing in XML node except BLOB Type.
Below what I did:
$rs = ibase_query("SELECT * FROM mytable");
$coln = ibase_num_fields($rs);
$fieldnames = array();
for ($i = 0; $i < $coln; $i++) {
$col_info = ibase_field_info($rs, $i);
$fieldnames[] = array('name' => $col_info['name'], 'type' => $col_info['type']);
}
$doc = new DOMDocument('1.0');
$sth = ibase_query($dbh, $stmt);
$doc->formatOutput = true;
$root = $doc->createElement('FA_ARTIKEL');
$root = $doc->appendChild($root);
while ($row = ibase_fetch_object($sth, IBASE_TEXT)) {
$title = $doc->createElement('RECORD');
$title = $root->appendChild($title);
$text = $doc->createTextNode('');
$text = $title->appendChild($text);
foreach ($fieldnames as $value) {
switch ($value['type']) {
case 'VARCHAR':
$rtitle = $doc->createElement($value['name']);
$rtitle = $title->appendChild($rtitle);
$rtext = $doc->createTextNode($row->$value['name']);
$rtext = $rtitle->appendChild($rtext);
break;
case 'BLOB':
$rbtitle = $doc->createElement($value['name']);
$rbtitle = $title->appendChild($rbtitle);
$rbtext = $doc->createTextNode($row->$value['name']);
$rbtext = $rbtitle->appendChild($rbtext);
break;
default:
if ($row->$value['name']) {
$rtitle = $doc->createElement($value['name']);
$rtitle = $title->appendChild($rtitle);
$rtext = $doc->createTextNode($row->$value['name']);
$rtext = $rtitle->appendChild($rtext);
} else {
$rtitle = $doc->createElement($value['name']);
$rtitle = $title->appendChild($rtitle);
$rtext = $doc->createTextNode('0');
$rtext = $rtitle->appendChild($rtext);
}
break;
}
}
}
Header('Content-type: text/xml');
echo $doc->saveXML() . "\n";
ibase_free_result($sth);
ibase_close($dbh);
I tried with SimpleXMLElement also but it also failed. What I am missing?
My Database is Firebird and I set BLOB fields as
BLOB SUB_TYPE 1 SEGMENT SIZE 16384
PHPs DOMDocument expects UTF-8 strings. It is possible that the blob contains control characters/invalid unicode sequences. Try to put the data that breaks the XML into a variable and reduce your problem to the absolute minimum.
$blobData = $record['blobField'];
$document = new DOMDocument();
$document
->appendChild($document->createElement('foo'))
->appendChild($document->createTextNode($blobData));
echo $document->saveXml();
This way you can see if the blob data is really the problem or merely a symptom.
If the BLOB contains binary data you will need to convert it into a TEXT format. Atom feeds for example urlencode binary data that they want to embed. In this you will need to decode the value in the reading program.
Related
I would like to be able to create an XML file from some of the content of a html page. I have tried intensively but seem to miss something.
I have created two arrays, I have setup a DOMdocument and I have prepared to save an XML file on the server... I have tried to make tons of different foreach loops all over the place - but it won't work.
Here is my code:
<?php
$page = file_get_contents('http://www.halfmen.dk/!hmhb8/score.php');
$doc = new DOMDocument();
$doc->loadHTML($page);
$score = $doc->getElementsByTagName('div');
$keyarray = array();
$teamarray = array();
foreach ($score as $value) {
if ($value->getAttribute('class') == 'xml') {
$keyarray[] = $value->firstChild->nodeValue;
$teamarray[] = $value->firstChild->nextSibling->nodeValue;
}
}
print_r($keyarray);
print_r($teamarray);
$doc = new DOMDocument('1.0','utf-8');
$doc->formatOutput = true;
$droot = $doc->createElement('ROOT');
$droot = $doc->appendChild($droot);
$dsection = $doc->createElement('SECTION');
$dsection = $droot->appendChild($dsection);
$dkey = $doc->createElement('KEY');
$dkey = $dsection->appendChild($dkey);
$dteam = $doc->createElement('TEAM');
$dteam = $dsection->appendChild($dteam);
$dkeytext = $doc->createTextNode($keyarray);
$dkeytext = $dkey->appendChild($dkeytext);
$dteamtext = $doc->createTextNode($teamarray);
$dteamtext = $dteam->appendChild($dteamtext);
echo $doc->save('xml/test.xml');
?>
I really like simplicity, thank you.
You need to add each item in one at a time rather than as an array, which is why I build the XML for each div tag rather than as a second pass. I've had to assume that your XML is structured the way I've done it, but this may help you.
$page = file_get_contents('http://www.halfmen.dk/!hmhb8/score.php');
$doc = new DOMDocument();
$doc->loadHTML($page);
$score = $doc->getElementsByTagName('div');
$doc = new DOMDocument('1.0','utf-8');
$doc->formatOutput = true;
$droot = $doc->createElement('ROOT');
$droot = $doc->appendChild($droot);
foreach ($score as $value) {
if ($value->getAttribute('class') == 'xml') {
$dsection = $doc->createElement('SECTION');
$dsection = $droot->appendChild($dsection);
$dkey = $doc->createElement('KEY', $value->firstChild->nodeValue);
$dkey = $dsection->appendChild($dkey);
$dteam = $doc->createElement('TEAM', $value->firstChild->nextSibling->nodeValue);
$dteam = $dsection->appendChild($dteam);
}
}
I've made this program that updates an xml file based on entries in an array.
I've used FILE_APPEND because the entries are more than one and otherwise file gets overwritten. But the problem is the xml version tag prints out as many times as many entries are there.
So i want to remove this tag.
Here's my program:-
<?php
include 'array.php';
$xmlW = new XMLWriter();
$file = 'entry-'. date('M-D-Y') .'.xml';
/*$setting = new XMLWriterSettings();
$setting->OmitXmlDeclaration = true;*/
foreach($data as $d) {
if(in_array ($d['Mode'], array('ccAV','MB','Paypal','E2P'))) {
$recordType = 'receipt';
$xml_object = simplexml_load_file ('receipt.xml');
} else {
$xml_object = simplexml_load_file ('journal.xml');
$recordType = 'journal';
}
$xml_object->xpath("/ENVELOPE/BODY/IMPORTDATA/REQUESTDATA/TALLYMESSAGE/VOUCHER")[0]->DATE = $d['InvoiceDate'];
$xml_object->xpath("/ENVELOPE/BODY/IMPORTDATA/REQUESTDATA/TALLYMESSAGE/VOUCHER")[0]->NARRATION = 'Rahul';
$xml_object->xpath("/ENVELOPE/BODY/IMPORTDATA/REQUESTDATA/TALLYMESSAGE/VOUCHER")[0]->EFFECTIVEDATE = $d['InvoiceDate'];
$xml_object->xpath("/ENVELOPE/BODY/IMPORTDATA/REQUESTDATA/TALLYMESSAGE/VOUCHER/ALLLEDGERENTRIES.LIST")[0]->LEDGERNAME = $d['Mode'];
$xml_object->xpath("/ENVELOPE/BODY/IMPORTDATA/REQUESTDATA/TALLYMESSAGE/VOUCHER/ALLLEDGERENTRIES.LIST")[0]->AMOUNT = 'Rahul';
$xml_object->xpath("/ENVELOPE/BODY/IMPORTDATA/REQUESTDATA/TALLYMESSAGE/VOUCHER/ALLLEDGERENTRIES.LIST")[1]->AMOUNT = 'Rahul';
$xml_object->xpath("/ENVELOPE/BODY/IMPORTDATA/REQUESTDATA/TALLYMESSAGE/VOUCHER/ALLLEDGERENTRIES.LIST")[2]->AMOUNT = 'Rahul';
$xml_object->xpath("/ENVELOPE/BODY/IMPORTDATA/REQUESTDATA/TALLYMESSAGE/VOUCHER/ALLLEDGERENTRIES.LIST/BANKALLOCATIONS.LIST")[0]->DATE = 'Rahul';
$xml_object->xpath("/ENVELOPE/BODY/IMPORTDATA/REQUESTDATA/TALLYMESSAGE/VOUCHER/ALLLEDGERENTRIES.LIST/BANKALLOCATIONS.LIST")[0]->INSTRUMENTDATE = 'Rahul';
$xml_object->xpath("/ENVELOPE/BODY/IMPORTDATA/REQUESTDATA/TALLYMESSAGE/VOUCHER/ALLLEDGERENTRIES.LIST/BANKALLOCATIONS.LIST")[0]->AMOUNT = 'Rahul';
$xml = $xml_object->asXML();
file_put_contents($file, $xml, FILE_APPEND);
}
?>
Thanks for the help.
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>
Hello I want to parse a HTML table and assign those values to php variables so that i can insert them in to mysql database
I have tried some html parsing methods like dom_html_parsing but as a beginer i am getting much confused, i would be gald if some provides me some hint on this so that i can code
Parsing code i used is
include('simple_html_dom.php');
$dom = str_get_html($result);
$table = array();
$html = str_get_html($result);
foreach($html->find('tr') as $row) {
$time = $row->find('td',-1)->plaintext;
$title = $row->find('td',0)->plaintext;
$title0 = $row->find('td',1)->plaintext;
$title1 = $row->find('td',2)->plaintext;
$title2 = $row->find('td',3)->plaintext;
$title3 = $row->find('td',4)->plaintext;
$title4 = $row->find('td',5)->plaintext;
$title5 = $row->find('td',6)->plaintext;
$table[$title][$title0][$title1][$title2][$title3][$title4][$title3] = true;
}
echo '<pre>';
print_r($table);
echo '</pre>';
the arrays are printing but i do not know how to insert those particular values in to mysql database, i want to assign those values to variables first so that i can insert in to the database and the format i need is shown above, name and fathername & htno are printed only once in html table but i need it to be repeated with each row of table
Please help me
$table_data = array();
$dom = new DOMDocument();
$dom->loadHTML($html_string);
$rows = $dom->getElementsByTagName('tr');
for ($i = 0; $i < $rows->length; $i++) {
$cells = $rows->item($i)->getElementsByTagName('td');
for ($j = 0; $j < $cells->length; $j++) {
$table_data[$i][$j] = $cells->item($j)->textContent;
}
}
//print_r($table_data);
You can use phpquery. It's similar to jQuery, but for PHP.
I am editing a xml file a particular node file after tha I am saving tha but it contains some special character because of line number 7 of my code
$xml = simplexml_load_file('demo.xml');
$i=2;
foreach($xml->Page as $myPage){
if($myPage['id']==$i) {
$da = "data";
$text = "helloworld";
$myPage->$da ="<![CDATA[{$text}]]>"; //line number
$xml->asXML('demo.xml');
}
how can I put the string as it is in xml file?
SimpleXML does not handle CDATA very well. If you want to write CDATA you need to use the DOM objects. For example:
$xml = new DOMDocument();
$xml->load('demo.xml');
$i = 2;
foreach ($xml->getElementsByTagName('Page') as $page) {
if ($page->attributes->getNamedItem('id')->value == $i) {
$da = 'data';
$text = 'helloworld';
$data = $xml->createElement($da);
$data->appendChild($xml->createCDATASection($text));
$page->appendChild($data);
}
}
If you want to continue to use SimpleXML, you can load just the element you want to write the CDATA into as a DOM object.
$xml = simplexml_load_file('demo.xml');
$i = 2;
foreach ($xml->Page as $page) {
if ($page['id'] == $i) {
$da = 'data';
$text = 'helloworld';
$page->$da = '';
$node = dom_import_simplexml($page->$da);
$dom = $node->ownerDocument;
$node->appendChild($dom->createCDATASection($text));
}
}
$xml->asXML('demo.xml');