php : xml node content edit and return xml

153 Views Asked by At

Here is my XML<response> <statusCode>200</statusCode> <statusText>OK</statusText> <data> <getAssetResponse> <assetId>89898</assetId> <content> some text with HTML content </content> </getAssetResponse> </data></response>

In my php, I need to replace content node substr (HTML with xhtml) and return the XML with same structure.

<?php $file = file_get_contents("filx.xml"); $doc = DOMDocument::loadXML($file); $data = $dom->getElementsByTagName("data"); foreach($data as $node){echo "hello";}

my simple start isn't working...What do i need to do to get the node content?

1

There are 1 best solutions below

2
On BEST ANSWER

If you only need to replace the content of the content-node, it is maybe easier to use SimpleXML, like this:

<?
$xml_object = simplexml_load_file("test.xml");
$xml_object->data->getAssetResponse->content = "Test 123";
print $xml_object->asXML();
?>

Result:

<?xml version="1.0"?>
  <response>
    <statusCode>200</statusCode>
    <statusText>OK</statusText>
    <data>
      <getAssetResponse>
        <assetId>89898</assetId>
        <content>Test 123</content>
      </getAssetResponse> 
    </data>
  </response>