これに関するいくつかの助けに感謝してください。データベースに挿入する80を超えるアイテムを含む非常に長いxmlフォームがあります。
私はこれを調査しましたが、何らかの理由でforeachループが機能しません。
私が何をしようとしているのかを理解するために、ここの挿入部分を減らしました。
最初の 'プロパティ/アイテムをデータベースに挿入できるので、挿入などに問題はありません。
何らかの理由で、ループはデータベース内の他の79項目を表示しません。
$affectedRow = 0;
$xml = simplexml_load_file('properties2.xml') or die("can not find it");
foreach($xml->children() as $row) {
$reference = $row->branch->properties->property['reference'];
$instructedDate = $row->branch->properties->property->instructedDate;
$price_text = $row->branch->properties->property->price_text;
$sql = "INSERT INTO test( reference, instructedDate, price_text) VALUES ('". $reference ."','". $instructedDate ."','". $price_text ."')";
$result = mysqli_query($conn, $sql);
if (! empty($result )) {
$affectedRow ++;
} else {
$error_message = mysqli_error($conn) . "\n";
}
}
?>
例:xmlファイル
-<agency branchName="billies Estate Agents " name="billie ea" xsi:noNamespaceSchemaLocation="http://www.feedcompany.co.uk/xmlexport/feedcompanyXMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<branches>
-<branch name="billies Estate Agents ">
-<properties>
-<property reference="00000477">
<instructedDate>26/02/2018 15:11:56</instructedDate>
<price_text>Offers in Excess of £200,000</price_text>
</property
-<property reference="00000478">
<instructedDate>26/02/2018 15:11:56</instructedDate>
<price_text>Offers in Excess of £200,000</price_text>
</property>
$xml->children()
期待したものではないため、最初の要素のみをDBに取得します。
XML<agency>
は<branches>
タグで始まり、タグがあることに注意してください。完全なXMLは次のようなものだと思います。
<agency>
<branches>
<branch>
<properties>
<property>
...
</property>
<property>
...
</property>
</properties>
</branch>
</branches>
</agency>
すべてのプロパティを取得する->なので、を使用する必要があります$xml->branches->branch->properties->children()
。
何かのようなもの:
foreach($xml->branches->branch->properties->children() as $property) {
$sql = "INSERT INTO test( reference, instructedDate, price_text) VALUES ('". $property['reference']."','". $property->instructedDate ."','". $property->price_text."')";
...
}
$xml->children()
3行目のように実行すると、branchsタグが配列内の唯一の要素として取得されます。そのため、DBに挿入される要素は1つだけです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加