2016-12-28 5 views
2

こんにちは誰かが私のPHPの下のXML文書の変更を手助けできますか?以下はPHPでXMLノードを追加する

この質問の主な理由は、私がイベントにServiceとしてタグ名を持つダミーのXMLノードを追加したいされて>>ボディ>>サービス>> 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にアレイ上のメンバ関数にaddChild()の呼び出し:\ XAMPP \ htdocsに\ xhe2e \ info.php線上
107行目は$ result = $ result-> addChild( 'Service'、 '');となります。

+1

を使用して付加子を取得することができていますが、あなたがしようとしているものを表示します。 http://stackoverflow.com/questions/31144422/append-xml-to-domnode-in-php?rq=1またはこの1つのような、関連する質問/回答のいずれかを読んだことがありますか:http: /stackoverflow.com/questions/40139917/append-child-to-xml-based-on-attribute-of-the-parent-node-in-php?rq=1 –

+0

@cale_b私は試しに質問を更新しました。私は問題を解決するために何をする必要があるかを手助けすることができます – Batman

答えて

1

あなたの試みで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()); 
+0

私は何もエラーが表示されていませんが、すべての応答も見ていません。 – Batman

+0

はい、ごめんなさい、それをエコーすることはできません - まずXMLに再度保存する必要があります。 –

+0

私はレスポンスにstring(33)というレスポンスを返します。代わりに 'var_dump'の代わりに' echo'を使用してレスポンスを文字列として取得したいのですが、ここでは役に立ちません。 – Batman

0
$sxe = new SimpleXMLElement($xmlstr); 
$result = $sxe->xpath('/Event/Body/Services/ServiceChanges/ServiceInstalls'); 
$result = $result[0]; 
$result = $result->addChild('Service','Dummy'); 

echo $sxe->asXML(); 

を、私はこのよう

関連する問題