2017-08-27 53 views
1

我必須從xml文件導入大約10 000個數據庫條目,但使用xsd文件數據結構,如何正確使用xsd文件導入xml數據?我使用PHP。從xsd數據結構解析xml數據?

這是我的XSD架構:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- edited with XMLSpy v2010 rel. 3 (x64) (http://www.altova.com) --> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> 
    <xs:element name="shopInformations"> 
     <xs:annotation> 
      <xs:documentation>All products</xs:documentation> 
     </xs:annotation> 
     <xs:complexType> 
      <xs:sequence minOccurs="0" maxOccurs="unbounded"> 
       <xs:element name="productInformation"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="title" type="xs:string"/> 
          <xs:element name="author" type="xs:string"/> 
          <xs:element name="productcode" type="xs:string" minOccurs="0"/> 
          <xs:element name="content"/> 
          <xs:element name="sections"> 
           <xs:complexType> 
            <xs:sequence maxOccurs="unbounded"> 
             <xs:element name="section"> 
              <xs:complexType> 
               <xs:sequence> 
                <xs:element name="title"/> 
               </xs:sequence> 
               <xs:attribute name="id" type="xs:string" use="required"/> 
              </xs:complexType> 
             </xs:element> 
            </xs:sequence> 
           </xs:complexType> 
          </xs:element> 
         </xs:sequence> 
         <xs:attribute name="type" use="required"> 
          <xs:simpleType> 
           <xs:restriction base="xs:string"> 
            <xs:enumeration value="public"/> 
            <xs:enumeration value="reseller"/> 
           </xs:restriction> 
          </xs:simpleType> 
         </xs:attribute> 
         <xs:attribute name="version" type="xs:string" use="required"/> 
         <xs:attribute name="lang" use="required"> 
          <xs:simpleType> 
           <xs:restriction base="xs:string"> 
            <xs:enumeration value="en"/> 
            <xs:enumeration value="es"/> 
           </xs:restriction> 
          </xs:simpleType> 
         </xs:attribute> 
        </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

我的xml文件有450MB左右,我無法打開它...

+1

方式過於寬泛,而不是一個的片段代碼在眼前!我猜你可以使用'DOMDocument'並遍歷節點,但是沒有看到數據,沒有人能夠真正提供答案。 – RamRaider

+0

你可以添加你當前擁有的任何代碼嗎?不是每個人都會爲你寫代碼。 –

+0

@NigelRen我現在添加了xsd示例,並嘗試使用DOMDocument,但是我找不到使用xsd作爲xml導入映射的方式... – nowilius

回答

0

由於細節是有點模糊,我不得不猜測相當有一點,但要閱讀這樣一個大文件,最好使用XMLReader來完成。 XMLReader允許您按分段讀取文件,而不是一次讀取整個文件。

下面的代碼顯示了讀取數據的一種簡單方法,但由於我必須從XSD創建一些測試數據 - 它可能不完全正確。

<?php 
error_reporting (E_ALL); 
ini_set ('display_errors', 1); 

$xml = new XMLReader; 
$xml->open('t1.xml'); 
while($xml->read()) { 
    if($xml->name == "productInformation") { 
     $product = $xml->readOuterXML(); 
     $prod = new SimpleXMLElement($product); 
     echo "title=".$prod->title.PHP_EOL; 
     echo "author=".$prod->author.PHP_EOL; 
     echo "productcode=".$prod->productcode.PHP_EOL; 
     echo "content=".$prod->content.PHP_EOL; 
     foreach ($prod->sections->section as $section) { 
      echo "section id=".$section['id'].PHP_EOL; 
      echo "section title=".$section->title.PHP_EOL; 
     } 

     echo PHP_EOL; 
     $xml->next(); 
    } 
} 

如果您打算使用由SimpleXML的返回值,您可能需要轉換的價值,所以$prod->title時被分配到一個字符串字段將需要(string)$prod->title

+0

這意味着我可以簡單地讀取xml而無需直接使用xsd文件? – nowilius

+0

xsd文件純粹是定義xml文件應符合的內容。您可以使用它來驗證您的XML,但不需要它來讀取它。 –