SlideShare a Scribd company logo
Dalton Valadares – Universidade Federal de Campina Grande
How to populate a DB reading values from a XML file, with GRAILS?
-Suppose your XML is the following:
<Result totalLooseOffers="0" xsi:schemaLocation="http://somewhere.onthe.net/something.xsd">
<details>
<applicationID>37394759ds2k9a2k467626f3d</applicationID>
<date>2013-04-11T14:31:11.490-03:00</date>
<elapsedTime>1</elapsedTime>
<status>success</status>
<code>0</code>
<message>success</message>
</details>
<category concatenateCategoryName="false" hasOffer="false" isFinal="true" parentCategoryId="116" id="3661">
<thumbnail url="http://somewhere.onthe.net/something.jpg"/>
<links>
<link type="category" url="http://somewhere.onthe.net/something/51922611"/>
<link type="xml" url="http://somewhere.onthe.net/something/3739416878456f4/br/>
</links>
<name>Air conditioning </name>
<filters>
<filter name="Brand" id="1246">
<value value="Springer Carrier" id="47204"/>
<value value="LG" id="7018"/>
<value value="Consul" id="7017"/>
<value value="Midea" id="106986"/>
</filter>
<filter name="Type" id="1246">
<value value="Split" id="404"/>
<value value="Window" id="18"/>
<value value="Portable" id="707"/>
<value value="Multi-split" id="1066"/>
</filter>
.
.
.
</category>
-You want to save filters and values into DB;
-With your domain and controller classes created, you will have the save() method implemented;
-Create the class XML_Handler:
import java.awt.GraphicsConfiguration.DefaultBufferCapabilities;
import entities.Filter; //Your domain class for Filters
import entities.Value; //Your domain class for Values
import groovy.util.XmlParser
class XML_Handler {
public static run() {
def air_conditioning = new File("/path_to_file/air_conditioning.xml")
this.insertIntoDB(air_conditioning)
}
static insertIntoDB(File file) {
def my_handler = new XmlParser().parse(file)
my_handler.category.filters.filter.each {
Filter filter = new Filter();
filter.description = it.@name
filter.save(flush: true)
println "Filter: " + it.@name + " -----OK!"
it.value.each {
//the attribute filter receives the filter inserted before (created above)...
Value value = new Value(description: it.@value, filter: filter);
value.save(flush: true)
println " Value - " + it.@value + " -----OK!"
}
}
}
-Now, you just need to put the line below to be executed in any part of any controller (you can put a
button to do this function):
XML_Handler.run(); //when the code is executed, the values are inserted into DB
I hope it can help...

More Related Content

PDF
Branding 2012
PDF
Apresentação sobre adaptação de topologia em redes móveis
PDF
Um Injetor de Falhas para a Avaliação de Aplicações Distribuídas Baseadas no ...
PDF
Programação C - Aula 2
PDF
Install and configure shiro plugin for authentication with Grails
PDF
Cobi t vs itil
KEY
Microformats - a quick primer
PPTX
Get Online MBA Education
Branding 2012
Apresentação sobre adaptação de topologia em redes móveis
Um Injetor de Falhas para a Avaliação de Aplicações Distribuídas Baseadas no ...
Programação C - Aula 2
Install and configure shiro plugin for authentication with Grails
Cobi t vs itil
Microformats - a quick primer
Get Online MBA Education

Similar to Xml to DB handler - grails tutorial (20)

PPTX
Semantic web support for POSH
PPTX
Microsoft Accademic Cloud Tour Genova
PPTX
OData: Universal Data Solvent or Clunky Enterprise Goo? (GlueCon 2015)
ZIP
Anex....,,,.
ZIP
Anex....,,,.
PPTX
Ilmu sosial dasar - Pembuatan CV - HTML
ODP
Simple run-time profiling quickie devoxx14
PDF
Data Con LA 2018 - Agile Integration Using an Enterprise Data Hub by Michael ...
PDF
Introduction to Bootstrap
PDF
Salesforce Lightning Data Services- Hands on Training
TXT
Check
PDF
Using Apache Solr
PPSX
Introduction to Html5
PPTX
Building Viral Social Experiences
PPTX
Devices + Cloud: Using Windows Azure with Windows Phone, iOS, Android, ...
PDF
Introduction to data visualisations with d3.js — Data Driven Documents
TXT
TXT
PPTX
Schematron
PDF
Beyond full-text searches with Lucene and Solr
Semantic web support for POSH
Microsoft Accademic Cloud Tour Genova
OData: Universal Data Solvent or Clunky Enterprise Goo? (GlueCon 2015)
Anex....,,,.
Anex....,,,.
Ilmu sosial dasar - Pembuatan CV - HTML
Simple run-time profiling quickie devoxx14
Data Con LA 2018 - Agile Integration Using an Enterprise Data Hub by Michael ...
Introduction to Bootstrap
Salesforce Lightning Data Services- Hands on Training
Check
Using Apache Solr
Introduction to Html5
Building Viral Social Experiences
Devices + Cloud: Using Windows Azure with Windows Phone, iOS, Android, ...
Introduction to data visualisations with d3.js — Data Driven Documents
Schematron
Beyond full-text searches with Lucene and Solr
Ad

More from Dalton Valadares (20)

PDF
Primeiros passos com Openstack
PDF
Performance Evaluation of an IEEE 802.11g Network in an Industrial Environment
PDF
802.11g Signal Strength Evaluation in an Industrial Environment (Elsevier Int...
PDF
Towards 802.11g Signal Strength Estimation in an Industrial Environment: a Pr...
PDF
Towards 802.11g Signal Strength Estimation in an Industrial Environment: a Pr...
PDF
Internet das Coisas e a Indústria 4.0
PDF
Achieving Data Dissemination with Security using FIWARE and Intel Software Gu...
PDF
Internet das Coisas com Edgex Foundry
TXT
OPTEE on QEMU - Build Tutorial
PDF
Presentation of my paper in the IEEE Symposium on Computer and Communications...
PDF
Avaliação de Desempenho de uma Rede 802.11g em uma Usina Termoelétrica
PDF
Apresentação sobre o modelo de segurança OPC UA
PDF
Modelo de segurança OPC UA
PDF
Introdução à Gestão de projetos
PDF
Integrating Fiware Orion, Keyrock and Wilma
PDF
Programação C - Aula 1
PDF
Programação C - Aula 3
PDF
Programação C - Aula 4
PDF
Desenvolvimento Web com JSF
PDF
Comparison of signal smoothing techniques for use in embedded system for moni...
Primeiros passos com Openstack
Performance Evaluation of an IEEE 802.11g Network in an Industrial Environment
802.11g Signal Strength Evaluation in an Industrial Environment (Elsevier Int...
Towards 802.11g Signal Strength Estimation in an Industrial Environment: a Pr...
Towards 802.11g Signal Strength Estimation in an Industrial Environment: a Pr...
Internet das Coisas e a Indústria 4.0
Achieving Data Dissemination with Security using FIWARE and Intel Software Gu...
Internet das Coisas com Edgex Foundry
OPTEE on QEMU - Build Tutorial
Presentation of my paper in the IEEE Symposium on Computer and Communications...
Avaliação de Desempenho de uma Rede 802.11g em uma Usina Termoelétrica
Apresentação sobre o modelo de segurança OPC UA
Modelo de segurança OPC UA
Introdução à Gestão de projetos
Integrating Fiware Orion, Keyrock and Wilma
Programação C - Aula 1
Programação C - Aula 3
Programação C - Aula 4
Desenvolvimento Web com JSF
Comparison of signal smoothing techniques for use in embedded system for moni...
Ad

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
A Presentation on Touch Screen Technology
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Approach and Philosophy of On baking technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
A Presentation on Artificial Intelligence
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Getting Started with Data Integration: FME Form 101
PPTX
1. Introduction to Computer Programming.pptx
PDF
Hybrid model detection and classification of lung cancer
PDF
Encapsulation theory and applications.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A Presentation on Touch Screen Technology
Heart disease approach using modified random forest and particle swarm optimi...
Approach and Philosophy of On baking technology
Programs and apps: productivity, graphics, security and other tools
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
A Presentation on Artificial Intelligence
Building Integrated photovoltaic BIPV_UPV.pdf
Web App vs Mobile App What Should You Build First.pdf
Zenith AI: Advanced Artificial Intelligence
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation_ Review paper, used for researhc scholars
Getting Started with Data Integration: FME Form 101
1. Introduction to Computer Programming.pptx
Hybrid model detection and classification of lung cancer
Encapsulation theory and applications.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Digital-Transformation-Roadmap-for-Companies.pptx

Xml to DB handler - grails tutorial

  • 1. Dalton Valadares – Universidade Federal de Campina Grande How to populate a DB reading values from a XML file, with GRAILS? -Suppose your XML is the following: <Result totalLooseOffers="0" xsi:schemaLocation="http://somewhere.onthe.net/something.xsd"> <details> <applicationID>37394759ds2k9a2k467626f3d</applicationID> <date>2013-04-11T14:31:11.490-03:00</date> <elapsedTime>1</elapsedTime> <status>success</status> <code>0</code> <message>success</message> </details> <category concatenateCategoryName="false" hasOffer="false" isFinal="true" parentCategoryId="116" id="3661"> <thumbnail url="http://somewhere.onthe.net/something.jpg"/> <links> <link type="category" url="http://somewhere.onthe.net/something/51922611"/> <link type="xml" url="http://somewhere.onthe.net/something/3739416878456f4/br/> </links> <name>Air conditioning </name> <filters> <filter name="Brand" id="1246"> <value value="Springer Carrier" id="47204"/> <value value="LG" id="7018"/> <value value="Consul" id="7017"/> <value value="Midea" id="106986"/> </filter> <filter name="Type" id="1246"> <value value="Split" id="404"/> <value value="Window" id="18"/> <value value="Portable" id="707"/> <value value="Multi-split" id="1066"/> </filter> . . . </category> -You want to save filters and values into DB; -With your domain and controller classes created, you will have the save() method implemented; -Create the class XML_Handler: import java.awt.GraphicsConfiguration.DefaultBufferCapabilities; import entities.Filter; //Your domain class for Filters import entities.Value; //Your domain class for Values import groovy.util.XmlParser class XML_Handler { public static run() { def air_conditioning = new File("/path_to_file/air_conditioning.xml") this.insertIntoDB(air_conditioning) } static insertIntoDB(File file) { def my_handler = new XmlParser().parse(file)
  • 2. my_handler.category.filters.filter.each { Filter filter = new Filter(); filter.description = it.@name filter.save(flush: true) println "Filter: " + it.@name + " -----OK!" it.value.each { //the attribute filter receives the filter inserted before (created above)... Value value = new Value(description: it.@value, filter: filter); value.save(flush: true) println " Value - " + it.@value + " -----OK!" } } } -Now, you just need to put the line below to be executed in any part of any controller (you can put a button to do this function): XML_Handler.run(); //when the code is executed, the values are inserted into DB I hope it can help...