0

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?

tv4free
  • 287
  • 3
  • 17

1 Answers1

0

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>
Pieter
  • 16
  • Thanks. That's great. But I want to find & replace...So, I tried the following...data->getAssetResponse->content; $new-data = str_replace("NEW", "OLD", $data-content); $xml_object->data->getAssetResponse->content = $new-data; print $xml_object->asXML(); ?> is not working – tv4free May 03 '12 at 20:41
  • That is weird.. The following code works perfect on my PC: ` $xml_object = simplexml_load_file("test.xml"); $content = $xml_object->data->getAssetResponse->content; $content = str_replace("Test", "TEST", $content ); $xml_object->data->getAssetResponse->content = $content; print $xml_object->asXML(); ?>` – Pieter May 04 '12 at 07:57