Page 1 of 1
xpath using external server content
Posted: Wed May 27, 2009 8:58 am
by tkoomzaaskz
Hello all, I'm new on this forum, hopefully I put the topic in the appropriate category (if not, please, don't blame me...)
The question is: how to use xpath to read HTML/XML content from external (non local) files? In Java I can do:
Code: Select all
URL url;
Document doc = new Document(url.openStream());
I've tried to find something like this, but failed to. I hope that downloading a file and parsing it locally is not the ONLY solution?
Many thanks in advance.
Tom
Re: xpath using external server content
Posted: Sun Jun 07, 2009 8:58 pm
by McInfo
Here is a simple example.
example.php
Code: Select all
<?php
header('Content-Type: text/plain');
$doc = new DOMDocument();
$doc->loadHTMLFile('http://127.0.0.1/example.html');
$xpath = new DOMXPath($doc);
foreach ($xpath->query('/html/body/p') as $paragraph) {
echo $paragraph->nodeValue."\n";
}
?>
If the server requires authentication, use this URL format: http://username:password@127.0.0.1/example.html
example.html
Code: Select all
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Example</title>
</head>
<body>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
<p>Paragraph 4</p>
</body>
</html>
PHP Manual:
Related topic:
100807
Edit: This post was recovered from search engine cache.