write xml from java file [closed]
My code have some errors on building xml structure. Can someone troubleshoot this code?
public class tulis{
public static void main(String[] args)throws IOException{
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("markers3.xml"));
sql_connection msconn=new sql_connection();
msconn.connect("gfsat", "root", "");
ResultSet rs=msconn.selectQuery("*", "location");
if(rs!=null)
{
try
{
while (rs.next())
{
bufferedWriter.write("<marker lat = \""+rs.getString(1)+ "\" lng = \"" +rs.getString(2)+ "\" \nhtml = 'Fish name:"+rs.getString(3)+"<br>Stock:"+rs.getString(4)+"tan metrik<br>Fish length"+rs.getString(5)+"cm<br>Status:"
+rs.getString(6)+ "<br>"+ "\n label = \""+rs.getString(7)+"\"\n/>");
bufferedWriter.newLine();
}
} catch (SQLException ex){
ex.printSt开发者_开发百科ackTrace();
}
msconn.disConnect();
bufferedWriter.flush();
bufferedWriter.close();
}
}
}
and here's the xml structure produce from above code..
<marker lat = "6.22353" lng = "102.111"
html = 'Fish name:ikan cencaru<br>Stock:1495.63343tan metrik<br>Fish length12cm<br>Status:matured<br>
label = "LKIM Geting"
/>
<marker lat = "5.90101" lng = "102.565"
html = 'Fish name:ikan bawal<br>Stock:1000tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Kuala Besar"
/>
<marker lat = "5.83095" lng = "102.563"
html = 'Fish name:ikan cermin/sagai/cupak<br>Stock:399.92534tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Kuala Besut"
/>
<marker lat = "5.32289" lng = "103.132"
html = 'Fish name:ikan kembung<br>Stock:10104.3163tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Pulau Kambing"
/>
<marker lat = "5.31688" lng = "103.171"
html = 'Fish name:ikan siakap<br>Stock:3.49643tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Chendering"
/>
<marker lat = "5.16911" lng = "103.229"
html = 'Fish name:ikan merah<br>Stock:266.25693tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Marang"
/>
<marker lat = "4.77798" lng = "103.425"
html = 'Fish name:ikan kerapu<br>Stock:369.48534tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Dungun"
/>
<marker lat = "4.24039" lng = "103.445"
html = 'Fish name:ikan parang<br>Stock:491.26436tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Kemaman"
/>
<marker lat = "3.79516" lng = "103.317"
html = 'Fish name:ikan jenahak<br>Stock:1000tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Kuantan"
/>
<marker lat = "3.53455" lng = "103.462"
html = 'Fish name:ikan duri<br>Stock:1000tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Kuala Pahang"
/>
<marker lat = "2.80024" lng = "103.487"
html = 'Fish name:ikan kembung<br>Stock:289.35158tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Kuala Rompin"
/>
<marker lat = "2.54508" lng = "103.66"
html = 'Fish name:ikan gelama/sengkerang<br>Stock:4826.84247tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Endau"
/>
<marker lat = "2.43594" lng = "103.836"
html = 'Fish name:ikan selar kuning<br>Stock:2940.80957tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Mersing"
/>
<marker lat = "1.34282" lng = "104.142"
html = 'Fish name:ikan selar<br>Stock:3372.49406tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Sedili"
/>
<marker lat = "2.38631" lng = "101.971"
html = 'Fish name:ikan selayang<br>Stock:1000tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Kuala Linggi"
/>
<marker lat = "3.00533" lng = "101.413"
html = 'Fish name:ikan kerisi bali<br>Stock:200.76684tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Pelabuhan Klang"
/>
<marker lat = "4.2111" lng = "100.641"
html = 'Fish name:ikan kerapu<br>Stock:969.48534tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Lumut"
/>
<marker lat = "5.68194" lng = "100.371"
html = 'Fish name:ikan siakap<br>Stock:3.49643tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Tanjung Dawai"
/>
<marker lat = "5.81496" lng = "100.371"
html = 'Fish name:ikan bawal<br>Stock:1000tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Sungai Udang"
/>
<marker lat = "5.41594" lng = "100.365"
html = 'Fish name:ikan selayang<br>Stock:10104.3163tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Batu Maung"
/>
<marker lat = "6.40548" lng = "100.414"
html = 'Fish name:ikan jenahak<br>Stock:1000tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Kuala Perlis"
/>
<marker lat = "1.57747" lng = "110.377"
html = 'Fish name:ikan bawal putih<br>Stock:4.92942tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Bintawa"
/>
<marker lat = "2.30282" lng = "111.273"
html = 'Fish name:ikan bawal hitam<br>Stock:1000tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Belawai"
/>
<marker lat = "2.50686" lng = "112.316"
html = 'Fish name:ikan gelama/tengkerong<br>Stock:289.35158tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Mukah"
/>
<marker lat = "2.225" lng = "111.335"
html = 'Fish name:ikan kerisi<br>Stock:5831.09568tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Tanjung Manis"
/>
<marker lat = "3.19783" lng = "113.091"
html = 'Fish name:ikan lolong<br>Stock:3735.66249tan metrik<br>Fish length8cm<br>Status:matured<br>
label = "LKIM Bintulu"
/>
<marker lat = "4.33737" lng = "113.995"
html = 'Fish name:ikan pelaling/temenong<br>Stock:34.42008tan metrik<br>Fish length13cm<br>Status:matured<br>
label = "LKIM Miri"
/>
Biggest error: you're trying to write the XML directly as strings, instead of using an XML API.
Never do that.
XML APIs will handle escaping, ensuring it's a valid file, closing all your tags etc. There are numerous XML APIs in Java - you may want to look at JDOM as a reasonably simple one.
精彩评论