How to import XML using PHP?

蹲街弑〆低调 提交于 2019-12-25 06:55:08

问题


I've been creating a simple website for a client (I'm a designer with front-end skills) but I've been thrown a curve ball and now they want a catalogue of products imported via a CSV or XML file. I've chosen the XML option as I have even less of a clue what to do with the CSV.

Would love some guidance on how I can start pulling out the XML using PHP.

XML product example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<style body="Neck%2C+waist+and+cuff+ribs+in+cotton%2FLycra%C2%AE%0ATwin+needle+stitching+throughout%0ATaped+back+neck%0A" brand="Fruit+of+the+Loom" code="SS8" date="20140303162801" footnote="Garment+sizes+are+approximate+and+for+guidance+only%0A" material="80%25+cotton+Belcoro%C2%AE+yarn%2F+20%25+polyester." name="Fruit+of+the+Loom+Raglan+Sweatshirt" type="Adult" weight="280+gsm">
<sizes>
    <row name="Size:">
        <col value="S"/>
        <col value="M"/>
        <col value="L"/>
        <col value="XL"/>
        <col value="XXL"/>
    </row>
    <row name="Chest (to fit):">
        <col value="35%2F37"/>
        <col value="38%2F40"/>
        <col value="41%2F43"/>
        <col value="44%2F46"/>
        <col value="47%2F49"/>
    </row>
</sizes>
<skus>
    <sku colour="BLK">
        <colours>
            <colour hex="0F1315" name="Black"/>
        </colours>
        <sizes>
            <size cartonPrice="490" cartonSize="36" name="S" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="36" name="M" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="36" name="L" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="36" name="XL" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="24" name="XXL" packPrice="520" packSize="6" singlePrice="560"/>
        </sizes>
    </sku>
    <sku colour="BOT">
        <colours>
            <colour hex="15451A" name="Bottle"/>
        </colours>
        <sizes>
            <size cartonPrice="490" cartonSize="36" name="S" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="36" name="M" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="36" name="L" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="36" name="XL" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="24" name="XXL" packPrice="520" packSize="6" singlePrice="560"/>
        </sizes>
    </sku>

....


回答1:


Please refer following code I think this will help you.

<!-- suppose this is book.xml file -->
<?xml version="1.0"?>
<catalog>
<book id="bk101">
  <author>Gambardella, Matthew</author>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <price>44.95</price>
  <publish_date>2000-10-01</publish_date>
  <description>An in-depth look at creating applications
  with XML.</description>
   </book>
   <book id="bk102">
  <author>Ralls, Kim</author>
  <title>Midnight Rain</title>
  <genre>Fantasy</genre>
  <price>5.95</price>
  <publish_date>2000-12-16</publish_date>
  <description>A former architect battles corporate zombies,
  an evil sorceress, and her own childhood to become queen
  of the world.</description>
   </book>
<catalog>

And this is your PHP:

<?php
// Loading the XML file
$xml = simplexml_load_file("books.xml");
echo "<h2>".$xml->getName()."</h2><br />";
foreach($xml->children() as $book)
{
    echo "BOOK : ".$book->attributes()->id."<br />";
    echo "Author : ".$book->author." <br />";
    echo "Title : ".$book->title." <br />";
    echo "Genre : ".$book->genre." <br />";
    echo "Price : ".$book->price." <br />";
    echo "Publish Date : ".$book->publish_date." <br />";
    echo "Description : ".$book->description." <br />";
    echo "<hr/>";
}
?>



回答2:


Of course you can use DOM in PHP. Given the books example, reading data into PHP:

<!-- suppose this is book.xml file -->
<?xml version="1.0"?>
<catalog>
<book id="bk101">
  <author>Gambardella, Matthew</author>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <price>44.95</price>
  <publish_date>2000-10-01</publish_date>
  <description>An in-depth look at creating applications
  with XML.</description>
   </book>
   <book id="bk102">
  <author>Ralls, Kim</author>
  <title>Midnight Rain</title>
  <genre>Fantasy</genre>
  <price>5.95</price>
  <publish_date>2000-12-16</publish_date>
  <description>A former architect battles corporate zombies,
  an evil sorceress, and her own childhood to become queen
  of the world.</description>
   </book>
<catalog>

Example: https://eval.in/117319

$dom = new DOMDocument();
$dom->loadXml('book.xml');
$xpath = new DOMXpath($dom);

$result = [];
foreach ($xpath->evaluate('//book') as $book) {
  $result[] = [
    'id' => $xpath->evaluate('string(@id)', $book),
    'Author' => $xpath->evaluate('string(author)', $book),
    'Title' => $xpath->evaluate('string(title)', $book),
    'Genre' => $xpath->evaluate('string(genre)', $book),
    'Price' => $xpath->evaluate('number(price)', $book),
    'Publish Date' => $xpath->evaluate('string(publish_date)', $book),
    'Description' => $xpath->evaluate('string(description)', $book)
  ];
}
var_dump($result);


来源:https://stackoverflow.com/questions/22301087/how-to-import-xml-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!