Reputation: 21
I have an XML file
<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>
I need to read this file and store the records to the database. Till now i have managed to upload and read the records however i cannot store them in my database. I have looked at this example which has similar XML format with my case but it does not work PHP - converting XML to array in PHP - parsing a soap xml in php and storing it in database
I am using CodeIgniter(below is my code)
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);
}
Any help please?
Upvotes: 0
Views: 2255
Reputation: 3149
Is your question regarding saving to the database, or accessing the elements in the XML?
I suspect the latter is the case, and the namespaces are throwing you off.
See the following example which accesses the elements from your SOAP response:
$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";
}
}
This outputs the following:
Code:prov
Description:Te Fresketa
LastModifiedDate:0001-01-01T00:00:00
active:true
After that, just save to the database as you like. Hope this helps!
Upvotes: 1