|
1 |
| -xmlquery |
| 1 | +phpQuery |
2 | 2 | ========
|
3 | 3 |
|
4 |
| -The XMLQuery class parses and manipulates XML or HTML documents in similar way as jQuery does. It uses CSS selectors instead of XPath. |
| 4 | +The phpQuery class is inspired by the great [jQuery](http://jquery.com/) library. The purpose of this class is to simplify the access and manipulation of XML documents. |
5 | 5 |
|
| 6 | +Instead of the XPath query language, this class uses CSS selectors. This is an advantage for those who are not familiar with the XPath language. In any case, it is possible to still use the XPath language: simply use the 'xpath' method instead of 'query' method. |
6 | 7 |
|
7 |
| -I. Creating instances: |
8 |
| -```php |
9 |
| -// creates an instance from a string |
10 |
| -$xml = new XMLQuery('<root>hello there</root>'); |
11 |
| - |
12 |
| -// creates an instance from an url |
13 |
| -$xml = new XMLQuery('http://www.php.net'); |
| 8 | +Installation |
| 9 | +------------ |
14 | 10 |
|
15 |
| -// creates an instance from a filename |
16 |
| -$xml = new XMLQuery('/home/username/my-file.xml'); |
17 |
| -``` |
| 11 | +Copy and paste the `classes` folder into your application and include the file 'classes/php-query.php'. That is: |
18 | 12 |
|
19 |
| -II. Traversing nodes: |
20 | 13 | ```php
|
21 |
| -$xml = new XMLQuery('<root><books><item id="1" title="One" /><item id="2" title="Two" /></books></root>'); |
22 |
| - |
23 |
| -// use the 'select' function to get nodes from a CSS expression |
24 |
| -// use the 'attr' function to get attributes from a node |
25 |
| -$items = $xml->select('books item'); |
26 |
| -foreach ($items as $item) { |
27 |
| - echo "Id: " . $item->attr("id") . ", Title: " . $item->attr("title") . "\n"; |
28 |
| -} |
29 |
| - |
30 |
| -// the previous example can also be written in the following and modern way |
31 |
| -// the functions 'select' and 'attr' are called internally |
32 |
| -$items = $xml('books item'); |
33 |
| -foreach ($items as $item) { |
34 |
| - echo "Id: {$item->id}, Title: {$item->title}\n"; |
35 |
| -} |
36 |
| - |
37 |
| -// gets the number of items |
38 |
| -echo "Number of items: " . count($items); |
| 14 | +use com\soloproyectos\core\xml\phpQuery; |
| 15 | +require_once "classes/php-query.php"; |
39 | 16 | ```
|
40 | 17 |
|
41 |
| -III. Manipulation: |
42 |
| -```php |
43 |
| -$xml = new XMLQuery('<root><books><item id="1" title="One" /><item id="2" title="Two" /></books></root>'); |
44 |
| -$books = $xml->select('books'); |
45 |
| - |
46 |
| -// changes an attribute and adds a new one |
47 |
| -$item = $books->select('item[id=2]'); |
48 |
| -$item->attr('title', 'Twenty Thousand Leagues Under the Sea'); |
49 |
| -$item->attr('author', 'Jules Verne'); |
| 18 | +And that's all. You are ready to use the CssSelector class. |
50 | 19 |
|
51 |
| -// changes the contents of an item |
52 |
| -$item->text('Look at my horse, my horse is amazing'); |
| 20 | +Basic Examples |
| 21 | +-------------- |
53 | 22 |
|
54 |
| -// inserts a new item to the end of the books node |
55 |
| -$books->append('<item id="3" title="Three" />'); |
| 23 | +### Creating instances: |
| 24 | +```php |
| 25 | +// loads an XML document from a string |
| 26 | +$query = new phpQuery('<root><item id="101" /><item id="102" /><item id="103" /></root>'); |
56 | 27 |
|
57 |
| -// inserts a new item to the beginning of the books node |
58 |
| -$books->prepend('<item id="0" title="Zero" />'); |
| 28 | +// loads an HTML document from a url |
| 29 | +$query = new phpQuery('http://www.php.net'); |
59 | 30 |
|
60 |
| -// removes an item |
61 |
| -$item = $books->select('item[id=2]'); |
62 |
| -$item->remove(); |
| 31 | +// loads an XML document from a file |
| 32 | +$query = new phpQuery('/home/username/my-file.xml'); |
63 | 33 |
|
64 |
| -// removes all content under the books node |
65 |
| -$books->clear(); |
| 34 | +// loads an XML document from a specific DOMNode object |
| 35 | +$doc = new DOMDocument("1.0", "UTF-8"); |
| 36 | +$doc->loadXML('<root><item id="101" /><item id="102" /><item id="103" /></root>'); |
| 37 | +$query = new phpQuery(doc); |
66 | 38 | ```
|
67 | 39 |
|
68 |
| -IV. Printing nodes |
| 40 | +### Traversing nodes: |
69 | 41 | ```php
|
70 |
| -$xml = new XMLQuery('<root><books><item id="1" title="One" /><item id="2" title="Two" /></books></root>'); |
71 |
| -$item = $xml->select('books item[id=2]'); |
| 42 | +$xml = new phpQuery("test.xml"); |
| 43 | + |
| 44 | +// prints books info |
| 45 | +$books = $xml->query("books item"); |
| 46 | +foreach ($books as $book) { |
| 47 | + echo "Title: " . $book->attr("title") . "\n"; |
| 48 | + echo "Author: " . $book->attr("author_id") . "\n"; |
| 49 | + echo "ISBN: " . $book->query("isbn")->text() . "\n"; |
| 50 | + echo "Available: " . $book->query("available")->text() . "\n"; |
| 51 | + echo "Description: " . trim($book->query("description")->text()) . "\n"; |
| 52 | + echo "---\n"; |
| 53 | +} |
72 | 54 |
|
73 |
| -// prints the string representation of a node |
74 |
| -echo $item->html(); |
| 55 | +// gets the number of items |
| 56 | +echo "Number of items: " . count($items); |
75 | 57 | ```
|
0 commit comments