Re: SimpleXML add a node

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



@richard
> I dunno about that whole try/catch thing, but if you're not going to USE
> it to catch the errors, then you might as well be old school and check the
> return values of all these functions.

that sounds good, but , so far I know, if the API says that a function _can_ 
throw an exception, I _must_ catch it. In this case 'importNode', 
'appendChild', and 'createElement'. If I'm wrong please correct me.

> Since it "works" once and not the second time, I'd be looking to see if
> the XML produced on the first go-around is actually VALID, since, if it's
> not, that would cause this problem, I think.

The first time 'dom_import_simplexml' returns a DOMElement, the second a 
DOMDocument. I don't know why.
The string version of the first SimpleXML is: (that produces a DOMElement)
<?xml version="1.0"?><config/>
The string version of the second SimpleXML is: (that produce a DOMDocument)
<?xml version="1.0"?><config><active/><config/>

@jason
I'm alredy using the 'dom_import_simplexml' function (line 5, code anexed)

@jochem
'instanceof' used now, thanks for reminding. Also if in this case it wasn't 
the prpblem.


last of all, a probalbly readable version of the code:
---
    // check if there is a valid simplexml document
    if (!($this->configXML instanceof SimpleXMLElement)) 
$this->createConfigXML();
       $dom = new DOMDocument('1.0');
       // create a DOMElement from SimpleXML
       $domElem = dom_import_simplexml($this->configXML);
       if ( $domElem === false ) return false;
  echo "\n\r".'1:';var_dump($domElem);echo "<BR>\n\r";
        try {
         // import DOMElement(simplexml) to empty DOMDocument
         $domNode = $dom->importNode($domElem,true);
         if ( $domNode === false ) return false;
          // append the imported node to the DOMDocument
         $domNode = $dom->appendChild($domNode);
          // create new DOMElement
          $domElem = $dom->createElement(($mode? 'softactive':'active'));
         // add new Element to DOMElement(simplexml)
          $domNode->appendChild( $domElem );
        } catch(Exception $e) {
          echo 'EXCEPTION: '.$e->getMessage().'<BR/>';
          return false;
        }
       // redefine old simplexml
       $simplexml = simplexml_import_dom($dom);
       if ($simplexml === false) return false;
       $this->configXML = $simplexml;
  echo '2:';echo $this->configXML->asXML();echo "<BR>\n\r";
    }
    return true;

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux