【问题标题】:Appending XML node in PHP在 PHP 中附加 XML 节点
【发布时间】:2016-12-28 15:53:36
【问题描述】:

你好,谁能帮我在 PHP 中修改下面的 XML 文档。

这个问题的主要原因是我想将标签名称为Service 的虚拟 XML 节点附加到 EVent>>Body>>Services>>ServiceInstalls

下面是 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;

错误是


致命错误:在 C:\xampp\htdocs\xhe2e\info.php 中的数组上调用成员函数 addChild()第 107 行
和第 107 行是 $result = $result->addChild('Service','');

【问题讨论】:

标签: php xml


【解决方案1】:

你在尝试中错过了一个微妙的项目:

$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());

【讨论】:

  • 现在我没有看到任何错误,但也没有看到任何响应。你能检查一下吗
  • 是的,抱歉,无法回显它 - 需要先将其再次保存为 XML。
  • 我在响应中得到字符串(33)的响应,而不是var_dump 我想使用echo 来得到字符串的响应,但它不工作你能在这里帮忙吗跨度>
  • 感谢@cale_b 我​​能够找到解决方案,感谢您的指导支持答案
【解决方案2】:
$sxe = new SimpleXMLElement($xmlstr);
$result = $sxe->xpath('/Event/Body/Services/ServiceChanges/ServiceInstalls');
$result = $result[0];
$result = $result->addChild('Service','Dummy');

echo $sxe->asXML();

我可以使用这种方式获取附加的孩子

【讨论】:

    猜你喜欢
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 2014-12-15
    • 2012-07-02
    • 1970-01-01
    相关资源
    最近更新 更多