从xsd数据结构中解析xml数据?

时间:2017-08-27 14:11:57

标签: php xml import xsd

我必须从xml文件导入大约10 000个数据库条目,但是使用xsd文件数据结构,如何正确使用xsd文件导入xml数据?我使用PHP。

这是我的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 个答案:

答案 0 :(得分: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