【问题标题】:XML SOAP save to mySQL databaseXML SOAP 保存到 mySQL 数据库
【发布时间】:2012-12-12 13:20:17
【问题描述】:

我有一个 XML 文件

<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

我正在使用 CodeIgniter(以下是我的代码)

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);

}

有什么帮助吗?

【问题讨论】:

    标签: php mysql xml codeigniter xml-parsing


    【解决方案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
    

    之后,只需根据需要保存到数据库即可。希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2016-08-01
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 2014-11-22
      • 2012-10-02
      • 2011-04-01
      • 2014-01-15
      • 1970-01-01
      相关资源
      最近更新 更多