2012-12-12 73 views
0

我有一個XML文件XML SOAP保存到MySQL數據庫

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header /> 
    <s:Body> 
    <GetAllItemCategoryResponse xmlns="http://tempuri.org/"> 
     <GetAllItemCategoryResult xmlns:a="http://schemas.datacontract.org/2004/07/HQService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <a:ItemsCategory> 
     <a:Code>prov</a:Code> 
     <a:Description>Te Fresketa</a:Description> 
     <a:LastModifiedDate>0001-01-01T00:00:00</a:LastModifiedDate> 
     <a:active>true</a:active> 
     </a:ItemsCategory>  
     </GetAllItemCategoryResult> 
    </GetAllItemCategoryResponse> 
    </s:Body> 
</s:Envelope> 

我需要閱讀該文件和記錄存儲到數據庫中。 直到現在我設法上傳和閱讀記錄,但是我不能將它們存儲在我的數據庫中。我已經看過這個例子也有類似的XML格式與我的情況,但它不工作我使用笨PHP - converting XML to array in PHP - parsing a soap xml in php and storing it in database

(下面是我的代碼)

function set_xml() 
{ 
    if ($this->input->post('submit')) 
    { 

     //uploading the file to the server 
     if (!empty($_FILES['userfile']['name'])){ 
      $this->upload->do_upload($_FILES['userfile']['name']); 
     } 

    } 

    $xml = realpath(APPPATH . '../public/').'/'.$_FILES['userfile']['name']; 

    $fh = fopen($xml,'r'); 
    $theData = fread($fh,filesize($xml)); 
    fclose($fh); 

      $element = new simpleXMLElement($theData); 
    $centerElement = $element->Body->GetAllItemCategoryResponse->GetAllItemCategoryResult->ItemsCategory; 

    $center = array(
     $centerElement->Code 
    ); 

    var_dump($centerElement); 

} 

任何幫助嗎?

回答

1

是關於保存到數據庫還是訪問XML中的元素?

我懷疑後者是這種情況,命名空間會把你扔掉。

請看下面的例子,其訪問您的SOAP響應元素:

$xml = file_get_contents(realpath(APPPATH . '../public/').'/'.$_FILES['userfile']['name']); 
$doc = simplexml_load_string($xml,NULL,false, "http://schemas.xmlsoap.org/soap/envelope/"); 
$doc->registerXPathNamespace('a', 'http://schemas.datacontract.org/2004/07/HQService'); 


foreach ($doc->xpath('//a:ItemsCategory') as $category) { 
    foreach ($category->children('http://schemas.datacontract.org/2004/07/HQService') as $child) { 
     echo $child->getName() . ":" . (string)$child . "\n"; 
    } 
} 

此輸出以下:

Code:prov 
Description:Te Fresketa 
LastModifiedDate:0001-01-01T00:00:00 
active:true 

之後,只需保存到數據庫中,只要你喜歡。希望這可以幫助!

+0

謝謝你的開發工作! –

+0

哇 - 你認真的搖出來找出它併發布 – cartalot