XML : PHP: How to change part of XML using DomElement

I am trying to make a function that changes part of an XML using XPath. I used part of someone else post:

  /*********************************************************************  Function to replace part of an XML  **********************************************************************/  function replacePartofXML($element, $methodName, $methodValue, $xml, $newPartofXML)  {      $xpathstring = "//" . $element . "[@$methodName = \"$methodValue\"]";      $xml->xpath($xpathstring);        //$domToChange = dom_import_simplexml($xml->xpath($xpathstring));      $domToChange = dom_import_simplexml($xml);      $domReplace  = dom_import_simplexml($newPartofXML);      $nodeImport  = $domToChange->ownerDocument->importNode($domReplace, TRUE);      $domToChange->parentNode->replaceChild($nodeImport, $domToChange);        return($xml);  }    

What I want to do is return the appended XML. I can't use dom_import_simplexml($xml->node->node) as my XML has many repeating element (but they have different ID reason why I am trying to use xpath)

The commented line does not work either as xpath returns an array and dom_import_simplexml is cannot import arrays.

Thanks for you input

No comments:

Post a Comment