【问题标题】:Update XML file (SimpleXmlElement Object) using string使用字符串更新 XML 文件(SimpleXmlElement 对象)
【发布时间】:2014-12-03 11:48:21
【问题描述】:

我有以下 XML 文件

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.6" jmeter="2.11 r1554548">
<hashTree>
<TestPlan ...>
  <elementProp name="TestPlan.user_defined_variables" ...>
    <collectionProp name="Arguments.arguments">

      <elementProp name="nb.publisher_Path" elementType="Argument">
        <stringProp name="Argument.name">nb.publisher_Path</stringProp>
        <stringProp name="Argument.value">http://mmm.com</stringProp>
        <stringProp name="Argument.metadata">=</stringProp>
      </elementProp> .... 

我想更新里面的 URL:elementProp name="nb.publisher_Path"

来自:http://mmm.com 至:http://aaa.com

所以我编写了以下 PHP 代码:

<?php
$xml = new SimpleXmlElement(file_get_contents("C://...Testing_User.jmx"));

    $result_publisher_Path_2 = $xml->xpath('//elementProp[@name="nb.publisher_Path"]/stringProp[@name="Argument.value"]')[0];

    $result_advertiser_Path = $xml->xpath('//elementProp[@name="nb.advertiser_Path"]/stringProp[2]');
    $result_adbrooker_Path = $xml->xpath('//elementProp[@name="nb.adbrooker_Path"]/stringProp[2]');
    echo ($result_publisher_Path_2);
    (string)$xml->xpath('//elementProp[@name="nb.publisher_Path"]/stringProp[@name="Argument.value"]')[0] = "http://aaa.com";
    echo $xml->asXml();

所以结果

echo ($result_publisher_Path_2);

是我要替换的正确 URL,当我尝试用

更改它时
(string)$xml->xpath('//elementProp[@name="nb.publisher_Path"]/stringProp[@name="Argument.value"]')[0] = "http://aaa.com";  

它没有改变。

有什么想法吗?

【问题讨论】:

  • 请将您的 XML 简化到最低限度。

标签: php xml xpath xml-parsing jmeter


【解决方案1】:

尝试使用这个:

$result_publisher_Path_2[0] = "http://aaa.com";

【讨论】:

  • 第二个例子是要走的路@sanki
  • @sanki : 谢谢 sanki,第二个是正确的 (Y)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
  • 2018-12-25
  • 2016-11-05
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
相关资源
最近更新 更多