2017-08-27 97 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,但不需要它来读取它。 –