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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
manickarthi
Forum Newbie
Posts: 4
Joined: Tue Mar 13, 2007 1:09 am

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

Post by manickarthi »

Hi,

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

Thaks in advance.[/b]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Might be something like

Code: Select all

$xpath->query('//comment()')
or preg_match or ... or ... so many possibilities.
Please be more specific.
manickarthi
Forum Newbie
Posts: 4
Joined: Tue Mar 13, 2007 1:09 am

Thanks help me again

Post 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()')
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

and I can't be more specific before you are more specific :)
manickarthi
Forum Newbie
Posts: 4
Joined: Tue Mar 13, 2007 1:09 am

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

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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";
}
manickarthi
Forum Newbie
Posts: 4
Joined: Tue Mar 13, 2007 1:09 am

Thanks for your help

Post by manickarthi »

Thank you very much volka, I got the solution.
Post Reply