Page 1 of 1

Reading Comment Lines(<!-- 2007 -->) from XML File usi

Posted: Tue Mar 13, 2007 1:19 am
by manickarthi
Hi,

I want to know, How we will read the XML File Comments using PHP Code.

Thaks in advance.[/b]

Posted: Tue Mar 13, 2007 3:22 am
by volka
Might be something like

Code: Select all

$xpath->query('//comment()')
or preg_match or ... or ... so many possibilities.
Please be more specific.

Thanks help me again

Posted: Tue Mar 13, 2007 9:02 am
by manickarthi
manickarthi wrote:Thanks for your reply, Please specify where will we give the XML file name in the below code

Code: Select all

$xpath->query('//comment()')

Posted: Tue Mar 13, 2007 9:15 am
by feyd

Posted: Tue Mar 13, 2007 10:37 am
by volka
and I can't be more specific before you are more specific :)

Reading Comment Lines(<!-- 2007 -->) from XML File usi

Posted: Tue Mar 13, 2007 11:45 pm
by manickarthi
My XML File Name:'hbwas51_proc_54.xml' this file contain the below content

<database>
<!-- 2006-12-27 15:45:00 IST / 1167214500 --> <row><v> 3.4102666667e+02 </v></row>
<!-- 2006-12-27 15:50:00 IST / 1167214800 --> <row><v> 3.4100000000e+02 </v></row>
<!-- 2006-12-27 15:55:00 IST / 1167215100 --> <row><v> 3.4100000000e+02 </v></row>
<!-- 2006-12-27 16:00:00 IST / 1167215400 --> <row><v> 3.4178333333e+02 </v></row>
<!-- 2006-12-27 16:05:00 IST / 1167215700 --> <row><v> 3.4101333333e+02 </v></row>
<!-- 2006-12-27 16:10:00 IST / 1167216000 --> <row><v> 3.5140000000e+02 </v></row>
<!-- 2006-12-27 16:15:00 IST / 1167216300 --> <row><v> 3.5140000000e+02 </v></row>
<!-- 2006-12-27 16:20:00 IST / 1167216600 --> <row><v> 3.5678666667e+02 </v></row>
<!-- 2006-12-27 16:25:00 IST / 1167216900 --> <row><v> 3.5581333333e+02 </v></row>
<!-- 2006-12-27 16:30:00 IST / 1167217200 --> <row><v> 3.5581333333e+02 </v></row>
<!-- 2006-12-27 16:35:00 IST / 1167217500 --> <row><v> 3.6214000000e+02 </v></row>
<!-- 2006-12-27 16:40:00 IST / 1167217800 --> <row><v> 3.6214000000e+02 </v></row>
<!-- 2006-12-27 16:45:00 IST / 1167218100 --> <row><v> 3.5512000000e+02 </v></row>
<!-- 2006-12-27 16:50:00 IST / 1167218400 --> <row><v> 3.5500000000e+02 </v></row>.
</database>


------------------------------------------
The below PHP code read the row tag values from the above xml file:

Code: Select all

<?php

$doc = new DOMDocument();

$doc->load( 'hbwas51_proc_54.xml' );

$rows = $doc->getElementsByTagName("row");
foreach($rows as $node) {
   print $node->textContent . " ";
}
Now i want to read the comment lines <!-- 2006-12-27 15:45:00 IST / 1167214500 --> from the above XML File.

Thanks for your help.

Posted: Wed Mar 14, 2007 1:22 am
by Kieran Huggins

Posted: Wed Mar 14, 2007 3:34 am
by volka
uh, why is this information in a comment and why is the only relationship "the comment is on the same line and before the <row> element"?
I wouldn't use the xml functions at all for this.

Code: Select all

<?php
$xml = "<database>
<!-- 2006-12-27 15:45:00 IST / 1167214500 --> <row><v> 3.4102666667e+02 </v></row>
<!-- 2006-12-27 15:50:00 IST / 1167214800 --> <row><v> 3.4100000000e+02 </v></row>
<!-- 2006-12-27 15:55:00 IST / 1167215100 --> <row><v> 3.4100000000e+02 </v></row>
<!-- 2006-12-27 16:00:00 IST / 1167215400 --> <row><v> 3.4178333333e+02 </v></row>
<!-- 2006-12-27 16:05:00 IST / 1167215700 --> <row><v> 3.4101333333e+02 </v></row>
<!-- 2006-12-27 16:10:00 IST / 1167216000 --> <row><v> 3.5140000000e+02 </v></row>
<!-- 2006-12-27 16:15:00 IST / 1167216300 --> <row><v> 3.5140000000e+02 </v></row>
<!-- 2006-12-27 16:20:00 IST / 1167216600 --> <row><v> 3.5678666667e+02 </v></row>
<!-- 2006-12-27 16:25:00 IST / 1167216900 --> <row><v> 3.5581333333e+02 </v></row>
<!-- 2006-12-27 16:30:00 IST / 1167217200 --> <row><v> 3.5581333333e+02 </v></row>
<!-- 2006-12-27 16:35:00 IST / 1167217500 --> <row><v> 3.6214000000e+02 </v></row>
<!-- 2006-12-27 16:40:00 IST / 1167217800 --> <row><v> 3.6214000000e+02 </v></row>
<!-- 2006-12-27 16:45:00 IST / 1167218100 --> <row><v> 3.5512000000e+02 </v></row>
<!-- 2006-12-27 16:50:00 IST / 1167218400 --> <row><v> 3.5500000000e+02 </v></row>
</database>";

$pattern = '#<!-- ([^/]+) / (\d+) --> <row><v>([^<]+)</v></row>#';
preg_match_all($pattern, $xml, $matches);

for($i=0; $i<count($matches[0]); $i++) {
	echo $matches[1][$i], ' -> ', $matches[3][$i], "<br />\n";
}

Thanks for your help

Posted: Wed Mar 14, 2007 4:51 am
by manickarthi
Thank you very much volka, I got the solution.