}
/* }}} */
-static xmlNodePtr sxe_get_element_by_offset(php_sxe_object *sxe, long offset, xmlNodePtr node) /* {{{ */
+static xmlNodePtr sxe_get_element_by_offset(php_sxe_object *sxe, long offset, xmlNodePtr node, long *cnt) /* {{{ */
{
long nodendx = 0;
-
+
if (sxe->iter.type == SXE_ITER_NONE) {
return NULL;
}
next_iter:
node = node->next;
}
+
+ if (cnt) {
+ *cnt = nodendx;
+ }
return node;
}
if (sxe->iter.type == SXE_ITER_CHILD) {
node = php_sxe_get_first_node(sxe, node TSRMLS_CC);
}
- node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node);
+ node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, NULL);
if (node) {
_node_as_zval(sxe, node, return_value, SXE_ITER_NONE, NULL, sxe->iter.nsprefix TSRMLS_CC);
}
int is_attr = 0;
int nodendx = 0;
int test = 0;
+ long cnt;
zval tmp_zv, trim_zv;
if (!member) {
if (elements) {
if (Z_TYPE_P(member) == IS_LONG) {
- newnode = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node);
+ newnode = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, &cnt);
if (newnode) {
++counter;
}
change_node_zval(newnode, value TSRMLS_CC);
} else if (counter > 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot assign to an array of nodes (duplicate subnodes or attr detected)");
- } else if (elements && !node) {
- xmlNewChild(mynode, mynode->ns, name, Z_STRVAL_P(value));
+ } else if (elements) {
+ if (!node) {
+ xmlNewTextChild(mynode, mynode->ns, name, Z_STRVAL_P(value));
+ } else if (Z_TYPE_P(member) == IS_LONG) {
+ if (cnt < Z_LVAL_P(member)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot add element %s number %ld when only %ld such elements exist", mynode->name, Z_LVAL_P(member), cnt);
+ }
+ xmlNewTextChild(mynode->parent, mynode->ns, mynode->name, Z_STRVAL_P(value));
+ }
} else {
if (attribs) {
switch (Z_TYPE_P(value)) {
if (sxe->iter.type == SXE_ITER_CHILD) {
node = php_sxe_get_first_node(sxe, node TSRMLS_CC);
}
- node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node);
+ node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, NULL);
}
else {
zval tmp_zv;
}
/* }}} */
-/* {{{ proto object SimpleXMLElement::children()
+/* {{{ proto object SimpleXMLElement::children([string ns])
Finds children of given node */
SXE_METHOD(children)
{
}
/* }}} */
+/* {{{ proto object SimpleXMLElement::getName()
+ Finds children of given node */
+SXE_METHOD(getName)
+{
+ php_sxe_object *sxe;
+ xmlNodePtr node;
+ int namelen;
+
+ sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+
+ GET_NODE(sxe, node);
+
+ namelen = xmlStrlen(node->name);
+ RETURN_STRINGL((char*)node->name, namelen, 1);
+}
+/* }}} */
+
/* {{{ proto array SimpleXMLElement::attributes([string ns])
Identifies an element's attributes */
SXE_METHOD(attributes)
SXE_ME(children, NULL, ZEND_ACC_PUBLIC)
SXE_ME(getNamespaces, NULL, ZEND_ACC_PUBLIC)
SXE_ME(getDocNamespaces, NULL, ZEND_ACC_PUBLIC)
+ SXE_ME(getName, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
--- /dev/null
+--TEST--
+SimpleXML: Adding an elements
+--SKIPIF--
+<?php if (!extension_loaded("simplexml")) print "skip"; ?>
+--FILE--
+<?php
+$xml =<<<EOF
+<people></people>
+EOF;
+
+function traverse_xml($xml, $pad = '')
+{
+ $name = $xml->getName();
+ echo "$pad<$name";
+ foreach($xml->attributes() as $attr => $value)
+ {
+ echo " $attr=\"$value\"";
+ }
+ echo ">" . trim($xml) . "\n";
+ foreach($xml->children() as $node)
+ {
+ traverse_xml($node, $pad.' ');
+ }
+ echo $pad."</$name>\n";
+}
+
+
+$people = simplexml_load_string($xml);
+traverse_xml($people);
+$people->person = 'Joe';
+$people->person['gender'] = 'male';
+traverse_xml($people);
+$people->person = 'Jane';
+traverse_xml($people);
+$people->person['gender'] = 'female';
+$people->person[1] = 'Joe';
+$people->person[1]['gender'] = 'male';
+traverse_xml($people);
+$people->person[3] = 'Minni-me';
+$people->person[2]['gender'] = 'male';
+traverse_xml($people);
+$people->person[3]['gender'] = 'error';
+
+?>
+===DONE===
+--EXPECTF--
+<people>
+</people>
+<people>
+ <person gender="male">Joe
+ </person>
+</people>
+<people>
+ <person gender="male">Jane
+ </person>
+</people>
+<people>
+ <person gender="female">Jane
+ </person>
+ <person gender="male">Joe
+ </person>
+</people>
+
+Warning: main(): Cannot add element person number 3 when only 2 such elements exist in %sext/simplexml/tests/027.php on line %d
+<people>
+ <person gender="female">Jane
+ </person>
+ <person gender="male">Joe
+ </person>
+ <person gender="male">Minni-me
+ </person>
+</people>
+
+Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference in %sext/simplexml/tests/027.php on line %d