简体   繁体   中英

php : xml node content edit and return xml

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM