2013-07-26 39 views
2

所有美好的一天。在使用crxml存儲庫時,不能正確生成xml文檔。這就是在文檔中添加新元素時發生的情況。行動方案: 首先我創建的文檔添加新商品時出錯

 $this->genXml->Item['Type'] = 'view'; 
     $this->genXml->Item->{'http://'.$this->siteUrll.'|Name'} = 'Last View'; 

     $this->genXml->Item->LastView->View->Time = $app['Time']; 
     $this->genXml->Item->LastView->View->Action = $app['Action']; 
     $this->genXml->Item->LastView->View->IP = $app['IP']; 
     return $this->genXml->xml(); 

,我得到這樣的XML文件的

<?xml version="1.0" encoding="UTF-8"?> 
<Item Type="view"> 
    <Name xmlns="http://sitename.com">Last View</Name> 
    <LastView> 
    <View> 
     <Time>11:45:12</Time> 
     <Action>Click</Action> 
     <IP>192.168.1.1</IP> 
    </View> 
    </LastView> 
</Item> 

進一步完成的結果添加新值

  $GetFile = <<<EOB 
     <?xml version="1.0" encoding="UTF-8"?> 
      <Item Type="view"> 
       <Name xmlns="http://sitename.com">Last View</Name> 
       <LastView> 
       <View> 
        <Time>11:45:12</Time> 
        <Action>Click</Action> 
        <IP>192.168.1.1</IP> 
       </View> 
       </LastView> 
      </Item> 
     EOB; 
      $this->genXml->loadXML($GetFile); 
      $this->genXml->Item->LastView->View[2]->Time = $app['Time']; 
      $this->genXml->Item->LastView->View[2]->Action = $app['Action']; 
      $this->genXml->Item->LastView->View[2]->IP = $app['IP']; 

      echo($this->genXml->xml()); 

,並得到錯誤代碼xml

<?xml version="1.0" encoding="UTF-8"?> 
<Item Type="view"> 
     <Name xmlns="http://sitename.com">Last View</Name> 
     <LastView> 
     <View> 
      <Time>11:45:12</Time> 
      <Action>Click</Action> 
      <IP>192.168.1.1</IP> 
     </View> 
     <View/><View><Time>11:45:12</Time><Action>Click</Action> <IP>192.168.1.1</IP></View></LastView> 
    </Item> 

即標籤爲

<View/>  

幫助解決輸出問題。也許我做錯了什麼? (對不起,我的英語,我知道我不如我想) 只需鏈接到repositorydescription的問題。

+0

誰低估了這個問題? –

+1

偏移量從0開始,而不是1;所以你應該添加'$ this-> genXml-> Item-> LastView-> View [1] - > Time'等等 –

+0

這只是代碼的一個例子。一般計數是這樣:的foreach($這 - > genXml->用品 - > LastView爲$密鑰=> $值) \t \t \t \t { \t \t \t \t \t $ I [] = $密鑰; \t \t \t \t} \t \t \t \t $ next_i =計數($ⅰ)+ 1; \t \t \t \t $ this-> genXml-> Item-> LastView-> View [$ next_i] - > Time = $ app ['Time']; – vitaly63

回答

1

由於它是一個典型的信息學問題,它有點搞笑。我們希望從0開始計數。您創建了ID爲0的第一個視圖(隱式)。如果您現在添加ID爲2的新視圖,則會丟失ID 1並插入空白視圖。結果從句法上是正確的。

您只需要將添加的視圖的索引更改爲1以防止出現這種情況。

+0

非常感謝。都是如此的初級。我也開始在圖書館本身犯罪。謝謝大家的幫助。 – vitaly63