PHP:XML节点内容编辑并返回XML


php : xml node content edit and return xml

这是我的XML <response> <statusCode>200</statusCode> <statusText>OK</statusText> <data> <getAssetResponse> <assetId>89898</assetId> <content> some text with HTML content </content> </getAssetResponse> </data></response>

在我的php中,我需要替换内容节点substr(HTML与xhtml)并返回具有相同结构的XML。

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

我的简单开始不起作用...我需要做什么才能获取节点内容?

如果你只需要替换content节点的内容,使用SimpleXML可能更容易,像这样:

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

结果:

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