大家好,有人可以帮我用 PHP 修改下面的 XML 文档吗?
这个问题的主要原因是我想附加一个虚拟 XML 节点,其标签名称为Service
到活动>>正文>>服务>>服务安装
下面是XML文档
<Event>
<Header>
<EventSource>TXT</EventSource>
</Header>
<Body>
<Services>
<CurrentServices>
<Service serviceID = "SS014">
<ServiceChangeActivity>NoChange</ServiceChangeActivity>
<TelephoneNumbers>
<TN></TN>
</TelephoneNumbers>
<Rate>0.00</Rate>
<Desc>Auto</Desc>
<Count>1</Count>
<LOB>XHS</LOB>
<PackageCode>Test</PackageCode>
<EPCServiceDef>
<EPCProduct pn = "10300029">
<Name>Auto</Name>
<LongDescription>Auto SERVICE</LongDescription>
<Type>Service</Type>
<LOB>Video</LOB>
</EPCProduct>
</EPCServiceDef>
<Type>S</Type>
<TypeClassification>S</TypeClassification>
<SubType>04</SubType>
<Status>C</Status>
<Provisionable>N</Provisionable>
<BillCode>Auto</BillCode>
<BillCodeDescription>Auto</BillCodeDescription>
<Outlet></Outlet>
<Port></Port>
<BeforeQuantity>1</BeforeQuantity>
<Quantity>1</Quantity>
<ConnectDate>2016-12-19</ConnectDate>
<CompleteIndicator>C</CompleteIndicator>
</Service>
<Service serviceID = "BA">
<ServiceChangeActivity>NoChange</ServiceChangeActivity>
<TelephoneNumbers>
<TN></TN>
</TelephoneNumbers>
<Rate>0.00</Rate>
<TXTServiceBilling>
<BeginDate>2016-12-19T00:00:00.000Z</BeginDate>
<Discount></Discount>
<DiscountDescription/>
<CustomerDiscount></CustomerDiscount>
<CustomerDiscountDescription/>
<DiscountGroup/>
<DiscountGroupBeginDate/>
<Charge>0.00</Charge>
<ChargeType>R</ChargeType>
<ChargeMethod></ChargeMethod>
<Hold/>
</TXTServiceBilling>
<Desc>Basic</Desc>
<Count>1</Count>
<LOB>Video</LOB>
<PackageCode>BA</PackageCode>
<EPCServiceDef>
<EPCProduct pn = "Auto">
<Name>Basic Video (B1)</Name>
<LongDescription>BASIC VIDEO</LongDescription>
<Type>Service</Type>
<LOB>Video</LOB>
</EPCProduct>
</EPCServiceDef>
<Type>S</Type>
<TypeClassification>S</TypeClassification>
<SubType>01</SubType>
<Status>C</Status>
<Provisionable>N</Provisionable>
<BillCode>BA</BillCode>
<BillCodeDescription>Basic</BillCodeDescription>
<Outlet></Outlet>
<Port></Port>
<BeforeQuantity>1</BeforeQuantity>
<Quantity>1</Quantity>
<ConnectDate>2016-12-19</ConnectDate>
<CompleteIndicator>C</CompleteIndicator>
<TXTServiceIdentifier>3</TXTServiceIdentifier>
</Service>
</CurrentServices>
<ServiceChanges>
<ServiceInstalls>
<Service serviceID = "SSSS">
<ServiceChangeActivity>Install</ServiceChangeActivity>
<Desc>SSSS</Desc>
<LOB>Other</LOB>
<TXTServiceIdentifier>4</TXTServiceIdentifier>
</Service>
</ServiceInstalls>
<ServiceDisconnects/>
</ServiceChanges>
</Services>
</Body>
</Event>
我已经尝试过这样的代码,但出现错误
$str只不过是在XML之上
$result = simplexml_load_string($str);
$result = $result->xpath('/Event/Body/Services/ServiceChanges/ServiceInstalls');
$result = $result->addChild('Service','');
echo $result;
Error is
致命错误:调用数组中的成员函数addChild()C:\xampp\htdocs\xhe2e\info.php在线的107
第 107 行是 $result = $result->addChild('Service','');
您在尝试中错过了一个微妙的项目:
$result = simplexml_load_string($str);
$result = $result->xpath('/Event/Body/Services/ServiceChanges/ServiceInstalls');
// $result should now be an array. Check to be sure:
if ( $result && is_array( $result ) ) {
// since it IS an array, set to the first element of the array
$result = $result[0];
// And NOW we can append
$result = $result->addChild('Service','');
}
// The part from here is only to make the output pretty-xml
// instead you can just use $result->saveXML()
$dom = new DOMDocument("1.0");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($result->saveXML());
var_dump($dom->saveXML());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)