PHP DOM function read line number from input file

I have a program that reads an XML file using DOM functions:

$doc = new DOMDocument('1.0');
$doc->load("myFile.xml");

      

As I traverse the nodes in this document, is there a way to determine which line in the input file a node was defined on?

For instance:

1: <!-- myFile.xml -->
2: <foobar>
3:     <foo>FOO</foo>
4:     <bar>BAR</bar>
5: </foobar>

      

and PHP:

$xp = new DOMXPath($doc);
$bars = $xp->query("//bar");
$myBar = $bars[0];
echo "The first <bar> element is on line " . performMagicHere(); // 4

      

+1


a source to share


2 answers


$element->getLineNo()

will return the line number of the open tag. Note: for opening tags that span more than one line, it will return the end of the open tag. It doesn't seem to work for DOMText nodes (returns 0).



http://us3.php.net/manual/en/domnode.getlineno.php

+2


a source


You cannot do this with the PHP DOM class. DOM Level 3 added support for this , but we don't have DOM Level 3 support in PHP yet.



+1


a source







All Articles