提取信息使用XPath
上一篇 / 下一篇 2008-01-31 17:07:02 / 个人分类:php->xml
来源:http://blog.phpman.info/archives/203
12.6.1问题
You want to make sophisticated queries of your XML data without parsing the document node by node.
你想要制造一个查询在你的XML数据和分解这个文件网点
12.6.2解答
Use XPath.
使用XPath
XPath is available in SimpleXML:
XPath是简单的可利用的
?php
$s = simplexml_load_file('address-book.xml');
$emails = $s->xpath('/address-book/person/email');
foreach ($emails as $email) {
// do something with $email
}
And in DOM:
然后在DOM
?php
$dom = new DOMDocument;
$dom->load('address-book.xml');
$xpath = new DOMXPath($dom);
$email = $xpath->query('/address-book/person/email');
foreach ($emails as $email) {
// do something with $email
}
12.6.3讨论
Except for the simplest documents, it’s rarely easy to access the data you want one element at a time. As your XML files become increasingly complex and your parsing desires grow, using XPath is easier than filtering the data inside a foreach.
除了简单的文件。它很容易去访问这个数据在你想要的一个元素。当作你的XML文件变成复杂的和你的分解要求生成。使用XPath是更加简单的过滤数据在一个foreach
PHP has an XPath class that takes a DOM object as its constructor. You can then search the object and receive DOM nodes in reply. SimpleXML also supports XPath, and it’s easier to use because it’s integrated into the SimpleXML object.
PHP有一个XPath类有一个DOM对象当作构造器。你可以搜索这个对象和接受DOM网点在这个回复。SimpleXML同样支持Xpath.然后容易使用因为它统一到一个SimpleXML对象
DOM supports XPath queries, but you do not perform the query directly on the DOM object itself. Instead, you create a DOMXPath object, as shown in Example 12-9.
DOM支持XPath查询。但是它不直接履行查询,例如在例子12-9
$dom = new DOMDocument;
$dom->load('address-book.xml');
$xpath = new DOMXPath($dom);
$email = $xpath->query('/address-book/person/email');
Instantiate DOMXPath by passing in a DOMDocument to the constructor. To execute the XPath query, call query( ) with the query text as your argument. This returns an iterable DOM node list of matching nodes (see Example 12-10).
例示DOMPath通过在一个DOMDocument 到这个构造器,去执行这个XPath查询。调用query()和query正文当作你的论点。它返回一个iterable DOM网点,看例子12-10
Using XPath with DOM in a basic example
$dom = new DOMDocument;
$dom->load('address-book.xml');
$xpath = new DOMXPath($dom);
$emails = $xpath->query('/address-book/person/email');
foreach ($emails as $e) {
$email = $e->firstChild->nodeValue;
// do something with $email
}
After creating a new DOMXPath object, query this object using DOMXPath::query( ), passing the XPath query as the first parameter (in this example, it’s /people/person/email). This function returns a node list of matching DOM nodes.
经过创造一个新的DOMXPath对象。查询这个对象使用DOMPath::query()。通过这个XPath query当作第一个参数
By default, DOMXPath::query( ) operates on the entire XML document. Search a subsection of the tree by passing in the subtree as a final parameter to query( ). For instance, to gather all the first and last names of people in the address book, retrieve all the people nodes and query each node individually, as shown in Example 12-11.
定义DOMXPath::query()操作在全部的XML文件。搜索一个分段的tree通过子树当作一个最后的参数到query(),例如集合所有的第一个和最后一个名字在这个住址名册。找回所有的people网点和查询每个个别的node,例如在例子12-11
$dom = new DOMDocument;
$dom->load('address-book.xml');
$xpath = new DOMXPath($dom);
$person = $xpath->query('/address-book/person');
foreach ($person as $p) {
$fn = $xpath->query('firstname', $p);
$firstname = $fn->item(0)->firstChild->nodeValue;
$ln = $xpath->query('lastname', $p);
$lastname = $ln->item(0)->firstChild->nodeValue;
print "$firstname $lastname\n";
}
David Sklar
Adam Trachtenberg
Inside the foreach, call DOMXPath::query( ) to retrieve the firstname and lastname nodes. Now, in addition to the XPath query, also pass $p to the method. This makes the search local to the node.
在foreach内部,调用DOMXPath::query()重新找回第一个名和最后一个名nodes,现在在除了XPath查询。同样通过$p到这个方法。它确定搜索本地node
In contrast to DOM, all SimpleXML objects have an integrated xpath( ) method. Calling this method queries the current object using XPath and returns a SimpleXML object containing the matching nodes, so you don’t need to instantiate another object to use XPath. The method’s one argument is your XPath query.
和DOM对比。所有的SimpleXML对象有一个综合的xpath()方法。调用这个方法查询这个正确的对象使用XPath和返回一个SimoleXML对象包含匹配的nodes。所以你不需要例示其他对象使用XPath。这个方法的一个论点是你的XPath查询
Use Example 12-12 to find all the matching email addresses in the sample address book.
使用例子12-12去寻找所有的匹配email地址。在这个住址名册
$s = simplexml_load_file('address-book.xml');
$emails = $s->xpath('/address-book/person/email');
foreach ($emails as $email) {
// do something with $email
}
This is shorter because there’s no need to dereference the firstNode or to take the nodeValue.
这个是简短的。因为它不需要废弃第一个站点或者获得价值
SimpleXML handles the more complicated example, too. Since xpath( ) returns SimpleXML objects, you can query them directly, as in Example 12-13.
SimpleXML也有很多复杂的例子。从xpath()返回Simple对象。你可以直接查询,例如在例子12-13
$s = simplexml_load_file('address-book.xml');
$people = $s->xpath('/address-book/person');
foreach($people as $p) {
list($firstname) = $p->xpath('firstname');
list($lastname) = $p->xpath('lastname');
print "$firstname $lastname\n";
}
David Sklar
Adam Trachtenberg
Since the inner XPath queries return only one element, use list to grab it from the array.
从这个内部的XPath查询返回唯一的元素。使用目录夺取它的数组
TAG:
