From: Dmitry Stogov Date: Thu, 18 Dec 2003 13:28:00 +0000 (+0000) Subject: two new methods were added X-Git-Tag: php-5.0.0b3RC2~61 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eb973da1d3397dfc30b19a10620b0e98aa385a62;p=php two new methods were added $node->count($subnode_name) - returns count of subnodes with specified name $node->attributes() - returns array of attributes --- diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c index e2a127a35b..8ec02a8ee6 100644 --- a/ext/simplexml/simplexml.c +++ b/ext/simplexml/simplexml.c @@ -930,6 +930,69 @@ SXE_METHOD(to_xml_file) } /* }}} */ +/* {{{ 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 @@ static zend_function_entry sxe_functions[] = { 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} }; diff --git a/ext/simplexml/tests/017.phpt b/ext/simplexml/tests/017.phpt new file mode 100644 index 0000000000..de2823c40c --- /dev/null +++ b/ext/simplexml/tests/017.phpt @@ -0,0 +1,76 @@ +--TEST-- +SimpleXML: iteration through subnodes +--SKIPIF-- + +--FILE-- + + + + + + + + + + +EOF; +$xml1 =<< + + + + +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--- diff --git a/ext/simplexml/tests/018.phpt b/ext/simplexml/tests/018.phpt new file mode 100644 index 0000000000..cc7153b7ce --- /dev/null +++ b/ext/simplexml/tests/018.phpt @@ -0,0 +1,65 @@ +--TEST-- +SimpleXML: iteration through subnodes and attributes +--SKIPIF-- + +--FILE-- + + + Text1 + + Text2 + + Text3 + + + + + + +EOF; +$xml1 =<< + + + + +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."\n"; + } +} + +traverse_xml("",simplexml_load_string($xml)); +echo "----------\n"; +traverse_xml("",simplexml_load_string($xml1)); +echo "---Done---\n"; +?> +--EXPECT-- + + + + + + + + + + + + +---------- + + + + +---Done--- \ No newline at end of file