I am trying to load xml file into mysql.
My XML looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<TEST>
<DATABASE>
<CUSTOMERS>
<CUSTOMER l_name="aa" f_name="aaaa">
<adr>
<street txt="mainstreet" zipcode="11111"/>
</adr>
<adr>
<street txt="secondstreet" zipcode="11111"/>
</adr>
</CUSTOMER>
<CUSTOMER l_name="bb" f_name="bbbb">
<adr>
<street txt="teststreet" zipcode="22222"/>
</adr>
</CUSTOMER>
<CUSTOMER l_name="cc" f_name="cccc"/>
</CUSTOMERS>
</DATABASE>
</TEST>
My code to import the file in the mysql is:
LOAD XML LOCAL INFILE '/Applications/MAMP/htdocs/uebung/customer.xml'
INTO TABLE customer
ROWS IDENTIFIED BY '<CUSTOMER>';
So it works. But if I have two addresses for a customer then it tooks only the last one in the database. That is, it just loads for the customer with the l_name = aa
the address with the second street. The address with mainstream isn't loaded in the DB.
How I have to change my MYSQL Code?
Using CUSTOMER as the record node: ROWS IDENTIFIED BY '<CUSTOMER>'
, each property tag inside of every <CUSTOMER>
tag must be unique, so if you declare the same node twice (in this case, <adr>
), the second node's value overwrites the first node's value. So you've basically nested too many times.
Try this instead:
<TEST>
<DATABASE>
<CUSTOMERS>
<CUSTOMER l_name="aa" f_name="aaaa">
<adr>
<street txt="mainstreet" zipcode="11111"/>
</adr>
</CUSTOMER>
<CUSTOMER l_name="aa" f_name="aaaa">
<adr>
<street txt="secondstreet" zipcode="11111"/>
</adr>
</CUSTOMER>
<CUSTOMER l_name="bb" f_name="bbbb">
<adr>
<street txt="teststreet" zipcode="22222"/>
</adr>
</CUSTOMER>
<CUSTOMER l_name="cc" f_name="cccc"/>
</CUSTOMERS>
</DATABASE>
</TEST>
Consider running XSLT in PHP to transform your original XML into the MySQL format you need.
XSLT (save as .xsl file, a special .xml file)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- IdentityTransform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="CUSTOMER[*[name(*)!='addr']]">
<xsl:apply-templates select="*" />
</xsl:template>
<xsl:template match="adr">
<xsl:element name="CUSTOMER">
<xsl:copy-of select="ancestor::CUSTOMER/@l_name"/>
<xsl:copy-of select="ancestor::CUSTOMER/@f_name"/>
<xsl:copy>
<xsl:copy-of select="*"/>
</xsl:copy>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
PHP (using php-xsl class)
// Set current path
$cd = dirname(__FILE__);
// Load the XML source and XSLT file
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->load($cd.'/Source.xml');
$xsl = new DOMDocument;
$xsl->load($cd.'/XSLTScript.xsl');
// Transform the original xml
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
$newXml = $proc->transformToXML($xml);
// Save output to file
$xmlfile = $cd.'/Output.xml';
file_put_contents($xmlfile, $newXml);
Output (use Output.xml in MySQL's LOAD DATA XML
command)
<TEST>
<DATABASE>
<CUSTOMERS>
<CUSTOMER l_name="aa" f_name="aaaa">
<adr>
<street txt="mainstreet" zipcode="11111"/>
</adr>
</CUSTOMER>
<CUSTOMER l_name="aa" f_name="aaaa">
<adr>
<street txt="secondstreet" zipcode="11111"/>
</adr>
</CUSTOMER>
<CUSTOMER l_name="bb" f_name="bbbb">
<adr>
<street txt="teststreet" zipcode="22222"/>
</adr>
</CUSTOMER>
<CUSTOMER l_name="cc" f_name="cccc"/>
</CUSTOMERS>
</DATABASE>
</TEST>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.