9

I've never really used the DOM parser before and now I have a question.

How would I go about extracting the URL from this markup:

<files>
    <file path="http://www.thesite.com/download/eysjkss.zip" title="File Name" />
</files>
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Belgin Fish
  • 19,187
  • 41
  • 102
  • 131

3 Answers3

13

Using simpleXML:

$xml = new SimpleXMLElement($xmlstr);
echo $xml->file['path']."\n";

Output:

http://www.thesite.com/download/eysjkss.zip
Alex Jasmin
  • 39,094
  • 7
  • 77
  • 67
  • 4
    Just be careful. The value of $xml->file['path'] isn't a string. It's an instance of SimpleXMLElement. – mellowsoon Oct 22 '10 at 02:28
  • 2
    Indeed. It can cause problems when comparing the value to another string but you can cast this value to a string beforehand `(string)$xml->file['path']` – Alex Jasmin Oct 22 '10 at 02:40
  • yeah but it's still an efficient way of doing it that I didn't know about, I still +1'ed your answer. – Belgin Fish Oct 22 '10 at 19:45
12

To do it with DOM you do

$dom = new DOMDocument;
$dom->load( 'file.xml' );
foreach( $dom->getElementsByTagName( 'file' ) as $file ) {
    echo $file->getAttribute( 'path' );
}

You can also do it with XPath:

$dom = new DOMDocument;
$dom->load( 'file.xml' );
$xPath = new DOMXPath( $dom );
foreach( $xPath->evaluate( '/files/file/@path' ) as $path ) {
    echo $path->nodeValue;
}

Or as a string value directly:

$dom = new DOMDocument;
$dom->load( 'file.xml' );
$xPath = new DOMXPath( $dom );
echo $xPath->evaluate( 'string(/files/file/@path)' );

You can fetch individual nodes also by traversing the DOM manually

$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->load( 'file.xml' );
echo $dom->documentElement->firstChild->getAttribute( 'path' );

Marking this CW, because this has been answered before multiple times (just with different elements), including me, but I am too lazy to find the duplicate.

ThW
  • 19,120
  • 3
  • 22
  • 44
Gordon
  • 312,688
  • 75
  • 539
  • 559
-1

you can use PHP Simple HTML DOM Parser,this is a php library。http://simplehtmldom.sourceforge.net/

Sam
  • 155
  • 2
  • 7
  • Why introduce a 3rd party library when the built-in features are more than adequate for this task? – Phil Oct 22 '10 at 02:40
  • It is like jquery,Very convenient – Sam Oct 22 '10 at 02:44
  • 1
    Suggested third party alternatives to [SimpleHtmlDom](http://simplehtmldom.sourceforge.net/) that actually use [DOM](http://php.net/manual/en/book.dom.php) instead of String Parsing: [phpQuery](http://code.google.com/p/phpquery/), [Zend_Dom](http://framework.zend.com/manual/en/zend.dom.html), [QueryPath](http://querypath.org/) and [FluentDom](http://www.fluentdom.org). – Gordon Oct 22 '10 at 10:32