这几天在做dickies数据迁移,品牌方给到的文件是XML格式,文件比较大,网上找了几个PHP读取文件的方法感觉都不好用,海哥推荐了一个,试了下,so good~
$reader = new XMLReader();
if (!$reader->open(ROOT_DIR.'/public/2017.xml')) {
die("Failed to open '2017.xml'");
}
$doc = new DOMDocument;
// move to the first <product /> node
while ($reader->read() && $reader->name !== 'customer');
while ($reader->name === 'customer')
{
$node = simplexml_import_dom($doc->importNode($reader->expand(), true));
$node['customer']['customer-name'] = str_replace("##", "&#", $node['customer']['customer-name']);// customer-name中有特殊字符解析不了,处理的时候将文件中的&#全部替换成了##,所以这个地方要重新替换
// now you can use $node without going insane about parsing
$node = json_decode(json_encode($node), 1);
$reader->next('customer');
}
$reader->close();
我的xml文件格式:
<?xml version="1.0" encoding="UTF-8"?>
<customers xmlns="http://www.demandware.com/xml/impex/customer/2006-10-31">
<customer customer-no="00001001">
<credentials>
<login>oliver.lijhds@bluecomgup.com</login>
<password encrypted="true" encryptionScheme="scrypt">$s0$b0401$ZtpBroFXeLeaB0q0KL/FTg==$jdB7/2IlopcHkd/lPk5ipNoP47vC1fpXk7uRoFgROME=</password>
<enabled-flag>true</enabled-flag>
<password-question/>
<password-answer/>
</credentials>
<profile>
<salutation/>
<title/>
<first-name/>
<second-name/>
<last-name>林原</last-name>
<suffix/>
<company-name/>
<job-title/>
<email>olivedddr.lin@bluecomgwwroup.com</email>
<phone-home/>
<phone-business/>
<phone-mobile>18621004667</phone-mobile>
<fax/>
<birthday>1989-03-30Z</birthday>
<gender>1</gender>
<creation-date>2016-12-27T05:43:17.000Z</creation-date>
<last-login-time>2017-06-15T03:17:26.000Z</last-login-time>
<last-visit-time>2017-06-15T03:17:26.000Z</last-visit-time>
<preferred-locale/>
</profile>
<note/>
</customer>
<customer customer-no="00001002">
<credentials>
<login>54633119181@qq.com</login>
<password encrypted="true" encryptionScheme="scrypt">$s0$b0401$22uuE1a1iD5G6pbwlQXodQ==$TvQUcs27S73RE/h5SiXrcfjgFi9RrqxuqSOGTFQXN/Q=</password>
<enabled-flag>true</enabled-flag>
<password-question/>
<password-answer/>
</credentials>
<profile>
<salutation/>
<title/>
<first-name/>
<second-name/>
<last-name>苏轩</last-name>
<suffix/>
<company-name/>
<job-title/>
<email>54632239181@qq.com</email>
<phone-home/>
<phone-business/>
<phone-mobile>18521185858</phone-mobile>
<fax/>
<birthday>1991-09-06Z</birthday>
<gender>1</gender>
<creation-date>2016-12-27T08:58:26.000Z</creation-date>
<last-login-time>2018-12-06T09:28:53.000Z</last-login-time>
<last-visit-time>2018-12-06T09:28:53.000Z</last-visit-time>
<preferred-locale/>
</profile>
<addresses>
<address address-id="上海市" preferred="true">
<salutation/>
<title/>
<first-name/>
<second-name/>
<last-name>苏轩</last-name>
<suffix/>
<company-name/>
<job-title/>
<address1>上海市虹口区四平路幸福村21210号502室</address1>
<address2/>
<suite/>
<postbox/>
<city>上海市</city>
<postal-code>200080</postal-code>
<state-code>上海</state-code>
<country-code>CN</country-code>
<phone>18521785858</phone>
<custom-attributes>
<custom-attribute attribute-id="district">虹口区</custom-attribute>
</custom-attributes>
</address>
<address address-id="上海市-1" preferred="false">
<salutation/>
<title/>
<first-name/>
<second-name/>
<last-name>王平</last-name>
<suffix/>
<company-name/>
<job-title/>
<address1>上海市嘉定区宝翔路158弄12112号6125</address1>
<address2/>
<suite/>
<postbox/>
<city>上海市</city>
<postal-code>402760</postal-code>
<state-code>上海</state-code>
<country-code>CN</country-code>
<phone>18521785858</phone>
<custom-attributes>
<custom-attribute attribute-id="district">嘉定区</custom-attribute>
</custom-attributes>
</address>
<address address-id="重庆市" preferred="false">
<salutation/>
<title/>
<first-name/>
<second-name/>
<last-name>王平</last-name>
<suffix/>
<company-name/>
<job-title/>
<address1>重庆市璧山区保健街3322号一单元</address1>
<address2/>
<suite/>
<postbox/>
<city>重庆市</city>
<postal-code>402760</postal-code>
<state-code>重庆</state-code>
<country-code>CN</country-code>
<phone>13668004679</phone>
<custom-attributes>
<custom-attribute attribute-id="district">璧山县</custom-attribute>
</custom-attributes>
</address>
</addresses>
<customer-groups>
<customer-group group-id="stuffmember"/>
</customer-groups>
<note/>
</customer>
</customers>