0

I need to insert data from my parsed XML file to mySQL table. Problem is that I have few attributes and don't know how to insert them in one row. I tried with updateString but it writes only last attribute.

Here is example from XML file:

<Tr rn=\"999999999999999\" vr=\"T\" sSpre=\"S\" reg=\"P\" dSpre=\"2010-09-30\" dOdprt=\"2000-01-01\" iban=\"SI56\" eno=\"R\" vir=\"R\" maticnaPps=\"00000000\"><Imetnik davcna=\"0000000\" matSub=\"0000000\" drz=\"705\"><PopolnoIme>xxx</PopolnoIme><KratkoIme>xxx</KratkoIme><Naslov sifTipNaslova=\"01\" sifObcina=\"039\" sifPosta=\"1303\" sifUlica=\"0000\" sifNaselje=\"059\" stHisna=\"027\" sifHsmid=\"11694551\"><Obcina>xxx</Obcina><Posta>xxx</Posta><Ulica>xxx</Ulica><Naselje>xxx</Naselje></Naslov></Imetnik></Tr> 

This is scratch from my java program that I used for writing in mySQL table.

if (myWorkLine.substring(0,4).equals(Tr)) { uprs.afterLast(); uprs.moveToInsertRow(); if (myWorkLine.contains(Tr)) { myWorkLine = myWorkLine.substring(myWorkLine.indexOf(Tr)+4); while (!myWorkLine.substring(0,1).equals("<")) { myTag = myWorkLine.substring(0,myWorkLine.indexOf("=")); myWorkLine = myWorkLine.substring(myWorkLine.indexOf("=")+2); myValue = myWorkLine.substring(0,myWorkLine.indexOf("\"")); myWorkLine = myWorkLine.substring(myWorkLine.indexOf("\"")+2); uprs.updateString("Tr",myTag + " " + myValue); if (myWorkLine.substring(0,myWorkLine.indexOf("\">")).indexOf(">") > 0) break; } } 

So once again, I need that in MySQL table column Tr contains attributes rn value, vr value, sSpre value,...

Thanks in advance.

P.S.: Please don't ask why I'm parsing XML file by this method, I had to do it that way. :)

2
  • I'm still going to ask why you're parsing XML that way. Java comes with inbuilt libs to parse XML for you. Doing it via String is error prone to say the least. Commented Nov 10, 2010 at 12:01
  • Yes, I know it comes with inbuilt parsers. My boss ordered me to do it that way. Commented Nov 10, 2010 at 13:12

1 Answer 1

1

Your code will repeatedly replace the "Tr" column with your concatenation of tag + " " + value so it'll only be the last one that goes in. Don't you perhaps want the different tags to go in different columns? Or maybe you need to continue concatenating and only call updateString at the end.

Could you post the desired table row for the given XML? That should help in determining what you are trying to achieve.

For example, if you just want to append them:

StringBuffer tr = new StringBuffer(); while (!myWorkLine.substring(0,1).equals("<")) { myTag = myWorkLine.substring(0,myWorkLine.indexOf("=")); myWorkLine = myWorkLine.substring(myWorkLine.indexOf("=")+2); myValue = myWorkLine.substring(0,myWorkLine.indexOf("\"")); myWorkLine = myWorkLine.substring(myWorkLine.indexOf("\"")+2); tr.append(myTag + " " + myValue).append(","); if (myWorkLine.substring(0,myWorkLine.indexOf("\">")).indexOf(">") > 0) break; } if (tr.length() > 0) { tr.deleteCharAt(tr.length()-1); // get rid of last comma } uprs.updateString("Tr",tr.toString()); 
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, this is exactly what I was looking for. Only thing I can't figure now is how to not include previous line from XML file. As now it appends values from previous line + current line.
Well, it is just a matter of clearing the StringBuffer (or creating a new one) when you detect the new line. From your code snippet it isn't clear when this condition would occur. I expected that the while loop was entered on the beginning of a line (with a freshly created StringBuffer) - from what you are saying this isn't the case.
Yes, you're right. I found it then that I need to clear StringBuffer. I did it that it clears everytime when while loop begins. Thanks for help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.