简体   繁体   中英

php : xml parse and return xml

XML parsing isn't working

<?php 
$input = "file1.xml";
$xml = simplexml_load_file($input);
$data-content = $xml->data->getAssetResponse->content;
$new-data = str_replace("NEW", "OLD", (string)$data-content);
$xml_object->data->getAssetResponse->content = $new-data;
print $xml_object->asXML(); 
?>

May I know why this isn't working?

Hyphens are not valid in PHP variable names. I suggest replacing them with underscores instead, as in $new_data and $data_content . Finally, you initialize $xml , but later are attempting to use the unknown variable $xml_object . Change those to $xml .

$input = "file1.xml";
$xml = simplexml_load_file($input);
$data_content = $xml->data->getAssetResponse->content;
$new_data = str_replace("text", "REPLACED STUFF", (string)$data_content);
$xml->data->getAssetResponse->content = $new_data;
print $xml->asXML(); 

// Outputs:
<response>
  <statusCode>200</statusCode> 
  <statusText>OK</statusText> 
  <data>
    <getAssetResponse> 
      <assetId>89898</assetId> 
      <content> some text with HTML content some REPLACED STUFF with HTML content </content>
    </getAssetResponse> 
  </data>
</response>

From the PHP documentation:

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*'

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