Reading XML in bash and importing in Mysql


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading XML in bash and importing in Mysql
# 1  
Old 04-11-2013
Question Reading XML in bash and importing in Mysql

Hi,
I'm not a pro bashscript writer but I'm learning and want to learn about my mistakes.
In the next script I have an error on rule 6 but I can't find what I'm doing wrong ...
I daily receive a file xml.xml and have to import it in an mysql database in a few existing tables.

Code:
#!/bin/bash
FILE="/transfer/store/xml.xml"
RESULT=$(xmlstarlet sel -t -v "count(//Appointment)" $FILE)
_DB_TABLE_PERSONS="persons"
_DB_TABLE_INVITATIONS="invitiations"
FOR (( i=0; i<$RESULT; i++ )) ; DO
 PFIRST_NAME="$(xmlstarlet sel -t -v //Appointment[${i}+1]/person-info/data1 $FILE)"
 PLAST_NAME="$(xmlstarlet sel -t -v //Appointment[${i}+1]/person-info/data2 $FILE)"
 PADDRESS="$(xmlstarlet sel -t -v //Appointment[${i}+1]/person-info/data3 $FILE)"
 PTELEPHONE="$(xmlstarlet sel -t -v //Appointment[${i}+1]/person-info/data4 $FILE)"
 PEMAIL="$(xmlstarlet sel -t -v //Appointment[${i}+1]/person-info/email $FILE)"
VALUES(NULL,'${FIRST_NAME}','${LAST_NAME}','${ADDRESS}','${TELEPHONE}','${EMAIL}');"
 QUERY="INSERT INTO ${_DB_TABLE_PERSONS}  ('id','first_name','last_name','address','telephone','email')  VALUES(NULL,'${PFIRST_NAME}','${PLAST_NAME}','${PADDRESS}','${PTELEPHONE}','${PEMAIL}');"
 mysql --host=10.1.12.69 --user=test --password=test --database=test < $QUERY
 ID_PERSONS=$(mysql --host=10.1.12.69 --user=test --password=test --database=test -se "SELECT LAST_INSERT_ID()")
 IREMARK="$(xmlstarlet sel -t -v //Appointment[${i}+1]/person-info/data5 $FILE)"
 ELAST_NAME="$(xmlstarlet sel -t -v //Appointment[${i}+1]/transfer-info/name $FILE)"
 ID_EMPLOYEES=$(mysql --host=10.1.12.69 --user=test --password=test  --database=test -se "SELECT id FROM employees WHERE  last_name=${ELAST_NAME}")
 LNAME="$(xmlstarlet sel -t -v //Appointment[${i}+1]/transfer-info/locatiebenaming $FILE)"
 ID_LOCATIONS=$(mysql --host=10.1.12.69 --user=test --password=test  --database=test -se "SELECT id FROM locations WHERE UCASE(name) like  '%${ELAST_NAME}%'")
 ID_I_TEMP="$(xmlstarlet sel -t -v //Appointment[${i}+1]/Date $FILE)"
 IT_I_TEMP="$(xmlstarlet sel -t -v //Appointment[${i}+1]/Time/Begin $FILE)"
 IDATE_INVITATION=${ID_I_TEMP:0:4}-${ID_I_TEMP:4:2}-${ID_I_TEMP:6:2}
 IEXPIRATION_DATE=${IT_I_TEMP:0:2}-${IT_I_TEMP:2:2}
 QUERY="INSERT INTO ${_DB_TABLE_INVITATIONS}  ('id','person','employee','location','date_invitation','time_invitation','notification','remark')   VALUES(NULL,'${PFIRST_NAME}','${PLAST_NAME}','${PADDRESS}','${PTELEPHONE}','${PEMAIL}');"
 mysql --host=10.1.12.69 --user=test --password=test --database=test < $QUERY
DONE

XML:
Code:
<?xml version="1.0" encoding="utf-8"?>
<transfer-export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <Appointment>
   <Date>20130409</Date>
   <Time>
     <Begin>1830</Begin>
     <End>1900</End>
   </Time>
   <person-info>
     <data1>bla</data1>
     <data2>m00</data2>
     <data3>street 10</data3>
     <data4>0000/000000</data4>
     <data5>subject</data5>
     <info>remarks</info>
     <email></email>
     <sms></sms>
   </person-info>
   <transfer-info>
     <dcode>BARBRE5</dcode>
     <name>TESTPERSON</name>
     <locatiebenaming>TESTLOCATION</locatiebenaming>
     <locatieID>3702</locatieID>
     <entryid>45784139</entryid>
   </transfer-info>
 </Appointment>
 <Appointment>
   <Date>20130412</Date>
   <Time>
     <Begin>1600</Begin>
     <End>1730</End>
   </Time>
   <person-info>
     <data1>bla2</data1>
     <data2>m002</data2>
     <data3>street 666</data3>
     <data4>0000000000</data4>
     <data5>andere</data5>
     <info></info>
     <email>mailaddress2</email>
     <sms></sms>
   </person-info>
   <transfer-info>
     <dcode>BIRBRE5</dcode>
     <name>TESTPERSON2</name>
     <locatiebenaming>TESTLOCATION2</locatiebenaming>
     <locatieID>5802</locatieID>
     <entryid>4578487</entryid>
   </transfer-info>
 </Appointment>
</transfer-export>

# 2  
Old 04-11-2013
Did you mean line 6? the for loop syntax in shell script should be in small letters
Code:
for ((...)); do
  ...
done

--ahamed
# 3  
Old 04-11-2013
On top of what ahamed101 says, this
Code:
VALUES(NULL,'${FIRST_NAME}','${LAST_NAME}','${ADDRESS}','${TELEPHONE}','${EMAIL}');"

I don't expect to work, nor this
Code:
mysql . . .  < $QUERY

, at least with the assignment to QUERY that you did in the line before. Should you have intended to use "here strings", use <<<
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

XML reading and parsing! Please help

See test.xml file attached (7 Replies)
Discussion started by: JulioAmerica
7 Replies

2. Shell Programming and Scripting

Large XML to MySQL - fast way

Hello, Sorry for my bad english. I need to improve performance in project managing large data, these data are exported to a MySql from XML. Now I use PHP (XMLReader ()) to do this job. I need a faster way to do this process. Which do you think is the best way? Example: (the item... (2 Replies)
Discussion started by: stendelis
2 Replies

3. Shell Programming and Scripting

Importing env from csh to bash

Hi All, In my account with csh shell, there are lots of env variables set and I want to import those all to bash in one stroke, is there any way to do it ? Thanks, D (1 Reply)
Discussion started by: Deei
1 Replies

4. Shell Programming and Scripting

Importing a path/file into a bash script

Hey, I'm new here. Basically, I'm trying to make a bash script that affects a file of my choice. What I want to do is $./script.sh /path/to/file.jpg and then the bash script will know that variable=/path/to/file.jpg Thanks! (4 Replies)
Discussion started by: TFB
4 Replies

5. Shell Programming and Scripting

Help with passing XML variables to MySQL DB via PHP

Hi everybody, I need the help of the Unix community once again :) I have some code which queries an XML feed and displays the results for me. I would like to enter the XML output in to my database, but I am having trouble passing the variables while INSERTing. Here is my code that I need to... (0 Replies)
Discussion started by: o0110o
0 Replies

6. Shell Programming and Scripting

importing functions from an external bash script

Hi all, I'm trying to import some functions I have saved in a file named functions.sh into a different script (protocol.sh). Can anybody show me how to do this? I tried source functions.sh as I would do at the terminal but source command cannot be found from within the script protocol.sh. ... (2 Replies)
Discussion started by: tevang
2 Replies

7. Solaris

Mysql query reading a text file.

Hi, Is there any way that mysql query reads the content from a text file which has data in the below format: 1,2,3,4,5 and selects matched data from another table. ie I have a table named xyz which has same ids as in that file. I want this query to get count of the ids from xyz file by... (6 Replies)
Discussion started by: jyothi_wipro
6 Replies

8. Shell Programming and Scripting

Reading only particular TAG from XML

Hi, I have an XML file with following structure. Between following tags I have pipedelimited records with newline characters (Data1|1|2|3) <!]> I need to read the data between above tags so that my output is a flat file with pipedelimited records. <BOS> <Header> <TTC>ABC</TTC> ... (9 Replies)
Discussion started by: dsrookie
9 Replies

9. Programming

importing xml: problem

I'm an absolute newbie for unix... For my work, I have to import a xml file in our system (jsp+sql) via putty telnet. Once i have copied the file in the right directory, I launch this command: ./thisImport -i input/thisImport/newimport.20071130.xml -l 10 -t this is a test (as you can see),... (1 Reply)
Discussion started by: tranky
1 Replies

10. Shell Programming and Scripting

Need help reading XML file

If someone out there could help me out with this problem. I would really appreciate it. My requirement is :Read XML and put data in Test.txt file.Using Shell script(bourn) My xml file: <root> <header> <HeaderData1>header1</HeaderData1> <HeaderData2>header2</HeaderData2>... (0 Replies)
Discussion started by: ram2s2001
0 Replies
Login or Register to Ask a Question