dmitry Thu Dec 18 08:28:01 2003 EDT
Added files:
/php-src/ext/simplexml/tests 017.phpt 018.phpt
Modified files:
/php-src/ext/simplexml simplexml.c
Log:
two new methods were added
$node->count($subnode_name) - returns count of subnodes with specified name
$node->attributes() - returns array of attributes
Index: php-src/ext/simplexml/simplexml.c
diff -u php-src/ext/simplexml/simplexml.c:1.96 php-src/ext/simplexml/simplexml.c:1.97
--- php-src/ext/simplexml/simplexml.c:1.96 Wed Dec 17 10:06:39 2003
+++ php-src/ext/simplexml/simplexml.c Thu Dec 18 08:27:59 2003
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: simplexml.c,v 1.96 2003/12/17 15:06:39 dmitry Exp $ */
+/* $Id: simplexml.c,v 1.97 2003/12/18 13:27:59 dmitry Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -930,6 +930,69 @@
}
/* }}} */
+/* {{{ simplexml_count()
+ */
+SXE_METHOD(count)
+{
+ char *name;
+ int name_len;
+ HashTable *subnodes;
+ zval **data_ptr;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) ==
FAILURE) {
+ return;
+ }
+ subnodes = sxe_properties_get(getThis() TSRMLS_CC);
+ if (zend_hash_find(subnodes, name, name_len+1, (void **) &data_ptr) ==
SUCCESS) {
+ if (Z_TYPE_PP(data_ptr) == IS_ARRAY) {
+ RETURN_LONG(zend_hash_num_elements(Z_ARRVAL_PP(data_ptr)));
+ } else {
+ RETURN_LONG(1);
+ }
+ } else {
+ RETURN_LONG(0);
+ }
+}
+/* }}} */
+
+/* {{{ simplexml_attributes()
+ */
+SXE_METHOD(attributes)
+{
+ php_sxe_object *sxe;
+ xmlNodePtr node;
+ xmlAttrPtr attr;
+ zval *value = NULL;
+ char *contents;
+
+ if (ZEND_NUM_ARGS() != 0) {
+ RETURN_FALSE;
+ }
+
+ sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+ GET_NODE(sxe, node);
+
+ array_init(return_value);
+ if (node) {
+ attr = node->properties;
+ while (attr) {
+ if (attr->name) {
+ MAKE_STD_ZVAL(value);
+ contents = xmlNodeListGetString((xmlDocPtr)
sxe->document->ptr, attr->children, 1);
+ ZVAL_STRING(value, contents, 1);
+ if (contents) {
+ xmlFree(contents);
+ }
+ add_assoc_zval_ex(return_value,
+ (char*)attr->name,
+ xmlStrlen(attr->name) + 1, value);
+ }
+ attr = attr->next;
+ }
+ }
+}
+/* }}} */
+
/* {{{ cast_object()
*/
static int
@@ -1536,6 +1599,8 @@
SXE_ME(next, NULL, ZEND_ACC_PUBLIC)
SXE_ME(hasChildren, NULL, ZEND_ACC_PUBLIC)
SXE_ME(getChildren, NULL, ZEND_ACC_PUBLIC)
+ SXE_ME(count, NULL, ZEND_ACC_PUBLIC)
+ SXE_ME(attributes, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
@@ -1590,7 +1655,7 @@
{
php_info_print_table_start();
php_info_print_table_header(2, "Simplexml support", "enabled");
- php_info_print_table_row(2, "Revision", "$Revision: 1.96 $");
+ php_info_print_table_row(2, "Revision", "$Revision: 1.97 $");
php_info_print_table_row(2, "Schema support",
#ifdef LIBXML_SCHEMAS_ENABLED
"enabled");
Index: php-src/ext/simplexml/tests/017.phpt
+++ php-src/ext/simplexml/tests/017.phpt
--TEST--
SimpleXML: iteration through subnodes
--SKIPIF--
<?php if (!extension_loaded("simplexml")) print "skip"; ?>
--FILE--
<?php
$xml =<<<EOF
<people>
<person name="Joe">
<child name="Ann" />
<child name="Marray" />
</person>
<person name="Boe">
<child name="Joe" />
<child name="Ann" />
</person>
</people>
EOF;
$xml1 =<<<EOF
<people>
<person name="Joe">
<child name="Ann" />
</person>
</people>
EOF;
function print_xml($xml) {
foreach($xml as $person) {
echo "person: ".$person['name']."\n";
foreach($person as $child) {
echo " child: ".$child['name']."\n";
}
}
echo "----------\n";
}
function print_xml2($xml) {
$persons = $xml->count("person");
for ($i=0;$i<$persons;$i++) {
echo "person: ".$xml->person[$i]['name']."\n";
$children = $xml->person[$i]->count("child");
for ($j=0;$j<$children;$j++) {
echo " child: ".$xml->person[$i]->child[$j]['name']."\n";
}
}
echo "----------\n";
}
print_xml(simplexml_load_string($xml));
print_xml(simplexml_load_string($xml1));
print_xml2(simplexml_load_string($xml));
print_xml2(simplexml_load_string($xml1));
echo "---Done---\n";
?>
--EXPECT--
person: Joe
child: Ann
child: Marray
person: Boe
child: Joe
child: Ann
----------
person: Joe
child: Ann
----------
person: Joe
child: Ann
child: Marray
person: Boe
child: Joe
child: Ann
----------
person: Joe
child: Ann
----------
---Done---
Index: php-src/ext/simplexml/tests/018.phpt
+++ php-src/ext/simplexml/tests/018.phpt
--TEST--
SimpleXML: iteration through subnodes and attributes
--SKIPIF--
<?php if (!extension_loaded("simplexml")) print "skip"; ?>
--FILE--
<?php
$xml =<<<EOF
<people>
<person name="Joe">
Text1
<child name="Ann" />
Text2
<child name="Marray" />
Text3
</person>
<person name="Boe">
<child name="Joe" />
<child name="Ann" />
</person>
</people>
EOF;
$xml1 =<<<EOF
<people>
<person name="Joe">
<child />
</person>
</people>
EOF;
function traverse_xml($pad,$xml) {
foreach($xml as $name => $node) {
echo $pad."<$name";
foreach($node->attributes() as $attr => $value) {
echo " $attr=\"$value\"";
}
echo ">\n";
traverse_xml($pad." ",$node);
echo $pad."</$name>\n";
}
}
traverse_xml("",simplexml_load_string($xml));
echo "----------\n";
traverse_xml("",simplexml_load_string($xml1));
echo "---Done---\n";
?>
--EXPECT--
<person name="Joe">
<child name="Ann">
</child>
<child name="Marray">
</child>
</person>
<person name="Boe">
<child name="Joe">
</child>
<child name="Ann">
</child>
</person>
----------
<person name="Joe">
<child>
</child>
</person>
---Done---
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php