2016-12-28 106 views
2

嗨有人可以幫助我在PHP中修改以下XML文檔。在PHP中追加XML節點

的主要原因是這個問題我想和標籤名稱的虛擬XML節點追加爲Service到事件>>車身>>服務>> 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; 

錯誤是


致命錯誤:調用一個成員函數的addChild()上陣列中C:\ XAMPP \ htdocs中\ xhe2e \上線info.php的
第107行是$ result = $ result-> addChild('Service','');

+1

顯示你試圖得到什麼附加的孩子。你有沒有讀過任何相關的問題/答案,如這一個:http://stackoverflow.com/questions/31144422/append-xml-to-domnode-in-php?rq=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

你錯過了在嘗試一個微妙的項目:

$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

我得到的答覆作爲字符串(33)在響應,而不是'var_dump'我想用'回聲'獲得響應作爲字符串,但它不工作你能幫助這裏 – 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(); 

我能夠用這種方式