Load XML into mySQL with diffrent rows ID - mysql

I have the following XML file and I need load into mysql using LOAD XML LOCAL INFILE.
I´m using the following line:
LOAD XML LOCAL INFILE 'C:/_BORBA_/xml/importxml.xml' INTO TABLE importxml ROWS IDENTIFIED BY '<ide>'
If I declare "ide" as delimiter, I can import with sucess, but I have to import all tags (ide, info and proc) as one record row.
How can I explicit that I have 3 groups of information into my XML file?
<?xml version="1.0" encoding="utf-8"?>
<infNFe versao="3.10" Id="NFe351710006">
<ide>
<cUF>35</cUF>
<cNF>99999</cNF>
<natOp>Venda de terceiros</natOp>
<indPag>1</indPag>
<mod>55</mod>
<serie>1</serie>
<nNF>888888</nNF>
<dhEmi>2017-10-26T16:50:52-02:00</dhEmi>
</ide>
<info>
<tpNF>1</tpNF>
<idDest>2</idDest>
<cMunFG>3525904</cMunFG>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
</info>
<proc>
<cDV>8</cDV>
<tpAmb>1</tpAmb>
<finNFe>1</finNFe>
<indFinal>0</indFinal>
<indPres>9</indPres>
<procEmi>3</procEmi>
<verProc>008</verProc>
<CNPJ>99999888844</CNPJ>
</proc>
</infNFe>
Thank you for any help.

Related

How to get rid of Liquibase includeAll tag with formatted .sql changesets error: Cannot find the declaration of element 'databaseChangeLog'

I am using a Gradle Spring project with liquibase. To run liquibase I am running the jar created by compiling the project. I am trying to use the liquibase "includeAll" tag in an xml changelog to run all formatted sql changelog scripts inside a directory I've called includeAllScriptsTest (currently contains only one .sql file named test.sql with one changeset).
If I try to use includeAll in my master db-changelog file, at run time liquibase returns the error: file:///.../conf/db/db.changelog-master.xml/ is not a recognized file type.
In an attempt to get around this, I reference from my db-changelog-master.xml another xml called includeAll-changelog.xml. In this file I have the includeAll tag, below are the contents of this file.
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="https://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog conf/xsd/dbchangelog-3.8.xsd">
<includeAll path="***/includeAllScriptsTest/" relativeToChangelogFile="true"/>
</databaseChangeLog>
Inside the folder includeAllScriptsTest is a single file called test.sql, with the contents as below:
--liquibase formatted sql
--changeset author:1 dbms:MySQL splitStatements:true endDelimiter://
DROP PROCEDURE IF EXISTS `TESTINCLUDEALL`;//
CREATE PROCEDURE `TESTINCLUDEALL`()
BEGIN
SELECT * FROM TABLE;
END;//
However, this gives me a different error at run time: cvc-elt.1.a: Cannot find the declaration of element 'databaseChangeLog'. I've found online conflicting information regarding whether it is possible to use includeAll with the version of liquibase used by my project. I'm currently using 3.4.1.
cvc-elt.1.a: Cannot find the declaration of element
'databaseChangeLog'
This error is an XML Parsing error, pointing databaseChangeLog is not declared in the xml schema.
May be you can try using following XSD in your changelog:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<includeAll path="***/includeAllScriptsTest/" relativeToChangelogFile="true"/>
</databaseChangeLog>
If this still doesn't help, please have a look at few points in answer on this post.

Read XML data and save them to mysql database in symfony 4

I have a simple XML file. I need to read the data and save them to mysql database table (1 or 2 tables). The file is like following :
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:g="http://">
<myfile>
<title><![CDATA[All data]]></title>
<stock>
<name><![CDATA[my name]]></name>
<qty><![CDATA[0]]></qty>
<price><![CDATA[4.99]]></price>
<image><![CDATA[http://fashiondropshippers.com/media/catalog/product/i/m/image_463.jpg]]></image>
</stock>
</myfile>
</rss>
I am trying to do that in Symfony 4 using crawler. my codes in my controller are following
$crawler = new Crawler();
$crawler->addContent(file_get_contents('http://localhost/XML/myxml.xml'));
foreach ($crawler as $domElement) {
var_dump($domElement->nodeValue);
}
return new JsonResponse($domElement->nodeValue);
It displays data with errors. Now I need to save those data in mysql database tables. Could you please tell me how to proceed further ?
Many thanks in advance !
Ok. If you want to save data to DB you have to do the following:
Add Doctrine to your project: https://symfony.com/doc/current/doctrine.html#installing-doctrine
Create an entity: https://symfony.com/doc/current/doctrine.html#creating-an-entity-class
Make migrations: https://symfony.com/doc/current/doctrine.html#migrations-creating-the-database-tables-schema and execute them
Save your data to DB https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database

How to insert this xml data to mysql using LOAD XML

<?xml version="1.0" encoding="UTF-8"?>
<jobs>
<job>
<location>Raleigh, NC, US</location>
<title>Implementation Consultant II</title>
<city>Raleigh</city>
<job_type/>
</job>
<job>
<location>Saint Louis, MO, US</location>
<title>Food Service Worker I</title>
<city>Raleigh</city>
<job_type> Permanent </job_type>
</job>
</jobs>
Here i cant able to insert the data for job_type into mysql using LOAD DATA LOCAL INFILE
It's a bug in MySQL. Upgrade to MySQL 5.5.46 or higher.
From official documentation:
Prior to MySQL 5.5.46, LOAD XML did not handle empty XML elements in
the form correctly. (Bug #67542, Bug #16171518)

How import xml files with mysql LOAD XML LOCAL INFILE

I have a xml file like this:
test.xml
<?xml version="1.0" encoding="utf-8" ?>
<plugin name="tree">
<title>Test</title>
<description>some description</description>
<files>
<file>test.tmp</file>
</files>
<install><![CDATA[
global $test;
]]></install>
<hooks>
<hook name="hookname"><![CDATA[
global $local;
]]></hook>
</hooks>
<phrases>
<phrase key="category"><![CDATA[Show categories]]></phrase>
</phrases>
</plugin>
and i like to import it into a MySQL Table like 'mytable'
CREATE TABLE mytable (plugin varchar(255),title varchar(255),description varchar(255), file varchar(255),install varchar(255),hook varchar(255),phrase varchar(255));
I used below command
LOAD XML LOCAL INFILE 'test.xml'
INTO TABLE mytable(plugin,title,description,file,install,hook,phrase);
it runs successfully but with 0 rows!
The query has been successfully implemented, 0 rows have been
affected.
Thank you
Include this line ROWS IDENTIFIED BY '<plugin>'. with that your query should look like
LOAD XML LOCAL INFILE "D:\\test.xml"
INTO TABLE mytable
ROWS IDENTIFIED BY '<plugin>';
Looks like your XML file formation is not correct and so even though 1 row gets inserted; all the values doesn't gets extracted (remains NULL).
Do little changes as below
Create table structure
CREATE TABLE mytable (
plugin_name varchar(255),
title varchar(255),
description varchar(255),
`file` varchar(255),
`install` varchar(255),
hook varchar(255),
phrase varchar(255));
Change your XML file
<?xml version="1.0" encoding="utf-8" ?>
<plugin plugin_name="tree">
<title>Test</title>
<description>some description</description>
<file>test.tmp</file>
<install>![CDATA[
global $test;
]]</install>
<hook name="hookname">![CDATA[
global $local;
]]</hook>
<phrase key="category">![CDATA[Show categories]]</phrase>
</plugin>
Now if you use
LOAD XML LOCAL INFILE "D:\\test.xml"
INTO TABLE mytable
ROWS IDENTIFIED BY '<plugin>';
All data gets extracted fine
Beside the answer given above; I just wanted to add that the database fields character casing must be same as that of the XML field casing. Or else load XML wouldn't be able to import that specific column(s).

Updating an attribute in XML with MySQL

I have a chunk of XML stored as a string in a MySQL database, and need to update one of the attributes using a query.
Given the following string:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<town>
<road name="Main" direction="north"/>
</town>
I would need to change it to update the attribute direction to a different value:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<town>
<road name="Main" direction="east"/>
</town>
Is there an easy way to accomplish this? Thanks in advance!
EDIT: The query would be ran in a SQL script file containing various other upgrade queries, which is called by a piece of code in Java.
Checkout PHP's simple XML
http://www.php.net/manual/en/class.simplexmlelement.php
$xml=new SimpleXMLElement($xml);
print $xml->road->attributes('direction') = 'east';
I ended up overriding the upgrade process for that version. Through Java and JDBC I looped through every row and used JAXB to gain access to the attribute that needed to be changed. I would have preferred to do it with full SQL queries. :(