Asked  7 Months ago    Answers:  5   Viewed   34 times

The "addChild" method of SimpleXMLElement seems like it should be the right choice, but it apparently only takes strings representing the tagname of the new child.

There's the object-ish notation for referencing nodes of the tree and setting them, e.g. $simpleXMLNode->child = value, but that only seems to work for simple text/numeric values. If I try the following:

$s = new SimpleXMLElement('<root/>');
$t = new SimpleXMLElement('<child/>');
$s->a = $t;
echo $s->asXML()

I get:

<?xml version="1.0"?>
<root><a></a></root>

when I was hoping for:

<?xml version="1.0"?>
<root><a><child/></a></root>

I thought of converting $t to a string and then adding it (after stripping out the XML declaration):

$s->a = substr($t->asXML(),22)

But this yields:

<?xml version="1.0"?>
<root><a>&lt;child/&gt;</a></root>

Again, not what I was hoping for.

Is there a typical way to accomplish this kind of thing with SimpleXML?

 Answers

53

Hey unknown. You have to use the DOMElement interface to your SimpleXML objects to achieve this.

<?php

$s = new SimpleXMLElement('<root/>');
$t = new DOMElement('child');

$dom = dom_import_simplexml($s);
$dom->appendChild($t);

echo $s->asXML();
// <root><child/></root>

If you need more specific details, let me know. There are several examples in the documentation and comments for the dom_import_simplexml() method too: http://php.net/dom_import_simplexml

Wednesday, March 31, 2021
 
Gilko
answered 7 Months ago
70

You have a network/DNS problem, not a PHP problem. Seems your machine cannot resolve the IP of api.flickr.com.

"su" to your web server user and try to resolve the name there, i.e.

$ sudo bash
  # get root
$ su - apache
  # we're the apache user now
$ ping api.flickr.com
Saturday, May 29, 2021
 
barden
answered 5 Months ago
55

As far as I know, you can't do it with SimpleXML because addChild doesn't make a deep copy of the element (being necessary to specify the tag name can easily be overcome by calling SimpleXMLElement::getName()).

One solution would be to use DOM instead:

With this function:

function sxml_append(SimpleXMLElement $to, SimpleXMLElement $from) {
    $toDom = dom_import_simplexml($to);
    $fromDom = dom_import_simplexml($from);
    $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}

We have for

<?php
header("Content-type: text/plain");
$sxml = simplexml_load_string("<root></root>");

$n1 = simplexml_load_string("<child>one</child>");
$n2 = simplexml_load_string("<child><k>two</k></child>");

sxml_append($sxml, $n1);
sxml_append($sxml, $n2);

echo $sxml->asXML();

the output

<?xml version="1.0"?>
<root><child>one</child><child><k>two</k></child></root>

See also some user comments that use recursive functions and addChild, e.g. this one.

Friday, July 16, 2021
 
Null
answered 3 Months ago
68

Cookies are simple text headers sent along with the request. CURL allows you to specify those directly using CURLOPT_COOKIE.

curl_setopt($ch, CURLOPT_COOKIE, 'key=value;anotherkey=anothervalue');

If you know what information to send, you can construct your own cookie header this way. The COOKIEJAR/COOKIEFILE options just automate parsing, saving and sending. You'll have to do that manually (read received Cookie headers, create Cookie headers to be send), if you don't want to write to a file.

Wednesday, August 11, 2021
 
u.gen
answered 2 Months ago
76

The Visual Studio Development Server is codenamed Cassini.

From ASP.NET 2.0: A Getting Started Guide

Cassini doesn't support virtual directories, security settings, 
or any of IIS's other fancy features; it's just a very simple web server 
that gives you the basics you need to get up and running.

I am using the IIS which is included with Windows XP Pro. It only allows one website without tweaking but does have virtual directories. If you are on Vista, you can setup multiple sites in IIS from what I have heard.

Sunday, August 15, 2021
 
David542
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :