Viewed   76 times
$value = $simpleXmlDoc->SomeNode->InnerNode;

actually assigns a simplexml object to $value instead of the actual value of InnerNode.

If I do:

$value = $simpleXmlDoc->SomeNode->InnerNode . "n";

I get the value. Anyway of getting the actual value without the clumsy looking . "n"?

 Answers

4

Cast as whatever type you want (and makes sense...). By concatenating, you're implicitly casting to string, so

$value = (string) $xml->someNode->innerNode;
Monday, August 15, 2022
5

You want to invoke the SimpleXMLElement's magic __toString method by casting it to string. Try: $data_to_save['title'] = (string)$picasa_feed->title;

Friday, September 16, 2022
 
5

The simple answer here is not to use print_r() with SimpleXML objects. Because they are wrappers around non-PHP data, functions like that which would normally show the "whole" object don't really reflect what you're looking at.

The way to access an attribute with SimpleXML is to use the attribute name as though it was an array key ($node['attribute']); this does not mean that there is an array somewhere with that key, it is a function-call in disguise.

If you want to get a feel for which nodes you're looking at while writing SimpleXML code, check out this simplexml_dump() function which I wrote (feedback welcome).

Tuesday, August 23, 2022
5

There might be ways to achieve what you want using only SimpleXML, but in this case, the simplest way to do it is to use DOM. The good news is if you're already using SimpleXML, you don't have to change anything as DOM and SimpleXML are basically interchangeable:

// either
$articles = simplexml_load_string($xml);
echo dom_import_simplexml($articles)->textContent;

// or
$dom = new DOMDocument;
$dom->loadXML($xml);
echo $dom->documentElement->textContent;

Assuming your task is to iterate over each <article/> and get its content, your code will look like

$articles = simplexml_load_string($xml);
foreach ($articles->article as $article)
{
    $articleText = dom_import_simplexml($article)->textContent;
}
Saturday, August 27, 2022
 
jasedit
 
1

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, August 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :