Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6686801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:12:50+00:00 2026-05-26T05:12:50+00:00

I’ve got an issue with SimpleXML. Like in the official documentation, I want to

  • 0

I’ve got an issue with SimpleXML. Like in the official documentation, I want to do this :

<?php
include 'example.php';
$movies = new SimpleXMLElement($xmlstr);

$movies->movie[0]->characters->character[0]->name = 'Miss Coder';

echo $movies->asXML();
?>

But my code is :

<?php
public function renderMarker($xml, &$html)
{
    $html = ((string) $html) . 'Text to add';
}
?>

with :

$html = object(SimpleXMLElement)#185 (1) {
  ["@attributes"]=>
  array(1) {
    ["id"]=>
    string(5) "title"
  }
}

But when I do this, I’ve got $html = string(12) "Text to add" as a result.
Does anybody knows a workarround for this.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:12:50+00:00Added an answer on May 26, 2026 at 5:12 am

    What you try to achieve does not work and there is no solution for that with SimpleXML.

    Each SimpleXML object has dynamic properties. You access them as-if they were a property of an object, but infact, each time you access them, either the property’s return value is given (reading) or something else is updated (writing).

    However, if you pass such properties to a function like you did, you only passed the actual return value (from reading) and not the property itself.

    Simpliefied example which does not work, because $xml->title is not passed as variable into the set_property function (there is no need to add a reference, it does not make a difference):

    $xmlStr = '<root><title></title></root>';
    
    $xml = new SimpleXMLElement($xmlStr);
    
    function set_property(&$prop)
    {
       $prop = 'Test';
    }
    
    set_property($xml->title);
    
    echo $xml->asXML();
    

    Output:

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

    As written, there is no easy workaround for it. One workaround is that you create yourself a SimpleXMLProperty object that you can pass around in function parameters:

    /**
     * Encapsulate SimpleXMLElement Property
     */
    class SimpleXMLProperty
    {
        private $obj, $prop;
        public function __construct(SimpleXMLElement $obj, $property)
        {
            $this->obj = $obj;
            $this->prop = $property;
        }
        public function get()
        {
            return $this->obj->{$this->prop};
        }
        public function set($value)
        {
            $this->obj->{$this->prop} = $value;
        }
    }
    
    
    $xmlStr = '<root><title></title></root>';
    
    $xml = new SimpleXMLElement($xmlStr);
    
    function set_property(SimpleXMLProperty $prop)
    {
       $prop->set('Test');
    }
    
    $property = new SimpleXMLProperty($xml, 'title');
    
    set_property($property);
    
    echo $xml->asXML();
    

    Output:

    <?xml version="1.0"?>
    <root><title>Test</title></root>
    

    It encapsulates the property and accesses it in read and write context depending on the get or set function called (Demo).

    But for what you build, it might be a better thing to look into DomDocument instead. It has a much more standardized interface to the DOM, and you actually pass objects already around, like text-nodes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
i got an object with contents of html markup in it, for example: string
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.