adamnet's avatar

Parse XML data into model

Hello everyone. Hello everyone. Does anyone have practical knowledge on how to import xml data into a database? Any example/instructions would be valuable.

0 likes
5 replies
JussiMannisto's avatar

Parse the data from the XML and write it to the database. You can use PHP's SimpleXML utilities for parsing.

I can't say much more with the given information.

DigitalArtisan's avatar

Depends on your data. Is your XML well formatted and do you have the XML Schema Definition (XSD) file?

Can be easy, and can be hard, again, depends on your data.

Jsanwo64's avatar

The normal flow is:

  1. load the XML

  2. loop through the nodes you need

  3. map each node to your model fields

  4. save with create() or updateOrCreate()

use App\Models\Product;

$xml = simplexml_load_file(storage_path('app/products.xml'));

foreach ($xml->product as $item) {
    Product::updateOrCreate(
        ['external_id' => (string) $item->id],
        [
            'name' => (string) $item->name,
            'price' => (float) $item->price,
            'description' => (string) $item->description,
        ]
    );
}

If your XML is looking like this

<products>
    <product>
        <id>1001</id>
        <name>Phone</name>
        <price>499.99</price>
        <description>Sample product</description>
    </product>
</products>

Good to note:

  1. Use SimpleXML for basic XML.

  2. Use XMLReader if the file is very large.

  3. If you have an XSD, validate the XML first before importing.

  4. Prefer updateOrCreate() instead of plain create() when records may already exist.

  5. Wrap large imports in a database transaction or process them in chunks.

  6. Add validation/casting because XML values come in as strings.

If the XML is nested or complex, you may need to flatten parts of it before saving.

A more complete Laravel-style approach would be something like:

$xml = simplexml_load_file($path);

foreach ($xml->items->item as $item) {
    YourModel::updateOrCreate(
        ['reference' => (string) $item->reference],
        [
            'title' => (string) $item->title,
            'amount' => (float) $item->amount,
            'published_at' => (string) $item->published_at ?: null,
        ]
    );
}

So basically, you can import XML into a database by parsing it first, then mapping each XML node to your model fields. In PHP or Laravel, simplexml_load_file() is a common starting point for small to medium files, while XMLReader is better for very large files. After parsing, loop through each record and save with create() or updateOrCreate(). I think it's best to show what your XML structure looks like, so it is easier to know what to suggest.

adamnet's avatar

Thank you so much all of you for giving me these information!! Do I have to install through composer some package?

jlrdw's avatar

SimpleXML is normally enabled by default, if not in your PHP, the PHP manual will explain how.

Please or to participate in this conversation.