开发者

write xml from java file [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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)+"&lt;br&gt;Stock:"+rs.getString(4)+"tan metrik&lt;br&gt;Fish length"+rs.getString(5)+"cm&lt;br&gt;Status:"
                                     +rs.getString(6)+ "&lt;br&gt;"+ "\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&lt;br&gt;Stock:1495.63343tan metrik&lt;br&gt;Fish length12cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Geting"
    />
    <marker lat = "5.90101" lng = "102.565" 
    html = 'Fish name:ikan bawal&lt;br&gt;Stock:1000tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Kuala Besar"
    />
    <marker lat = "5.83095" lng = "102.563" 
    html = 'Fish name:ikan cermin/sagai/cupak&lt;br&gt;Stock:399.92534tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Kuala Besut"
    />
    <marker lat = "5.32289" lng = "103.132" 
    html = 'Fish name:ikan kembung&lt;br&gt;Stock:10104.3163tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Pulau Kambing"
    />
    <marker lat = "5.31688" lng = "103.171" 
    html = 'Fish name:ikan siakap&lt;br&gt;Stock:3.49643tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Chendering"
    />
    <marker lat = "5.16911" lng = "103.229" 
    html = 'Fish name:ikan merah&lt;br&gt;Stock:266.25693tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Marang"
    />
    <marker lat = "4.77798" lng = "103.425" 
    html = 'Fish name:ikan kerapu&lt;br&gt;Stock:369.48534tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Dungun"
    />
    <marker lat = "4.24039" lng = "103.445" 
    html = 'Fish name:ikan parang&lt;br&gt;Stock:491.26436tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Kemaman"
    />
    <marker lat = "3.79516" lng = "103.317" 
    html = 'Fish name:ikan jenahak&lt;br&gt;Stock:1000tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Kuantan"
    />
    <marker lat = "3.53455" lng = "103.462" 
    html = 'Fish name:ikan duri&lt;br&gt;Stock:1000tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Kuala Pahang"
    />
    <marker lat = "2.80024" lng = "103.487" 
    html = 'Fish name:ikan kembung&lt;br&gt;Stock:289.35158tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Kuala Rompin"
    />
    <marker lat = "2.54508" lng = "103.66" 
    html = 'Fish name:ikan gelama/sengkerang&lt;br&gt;Stock:4826.84247tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Endau"
    />
    <marker lat = "2.43594" lng = "103.836" 
    html = 'Fish name:ikan selar kuning&lt;br&gt;Stock:2940.80957tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Mersing"
    />
    <marker lat = "1.34282" lng = "104.142" 
    html = 'Fish name:ikan selar&lt;br&gt;Stock:3372.49406tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Sedili"
    />
    <marker lat = "2.38631" lng = "101.971" 
    html = 'Fish name:ikan selayang&lt;br&gt;Stock:1000tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Kuala Linggi"
    />
    <marker lat = "3.00533" lng = "101.413" 
    html = 'Fish name:ikan kerisi bali&lt;br&gt;Stock:200.76684tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Pelabuhan Klang"
    />
    <marker lat = "4.2111" lng = "100.641" 
    html = 'Fish name:ikan kerapu&lt;br&gt;Stock:969.48534tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Lumut"
    />
    <marker lat = "5.68194" lng = "100.371" 
    html = 'Fish name:ikan siakap&lt;br&gt;Stock:3.49643tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Tanjung Dawai"
    />
    <marker lat = "5.81496" lng = "100.371" 
    html = 'Fish name:ikan bawal&lt;br&gt;Stock:1000tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Sungai Udang"
    />
    <marker lat = "5.41594" lng = "100.365" 
    html = 'Fish name:ikan selayang&lt;br&gt;Stock:10104.3163tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Batu Maung"
    />
    <marker lat = "6.40548" lng = "100.414" 
    html = 'Fish name:ikan jenahak&lt;br&gt;Stock:1000tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Kuala Perlis"
    />
    <marker lat = "1.57747" lng = "110.377" 
    html = 'Fish name:ikan bawal putih&lt;br&gt;Stock:4.92942tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Bintawa"
    />
    <marker lat = "2.30282" lng = "111.273" 
    html = 'Fish name:ikan bawal hitam&lt;br&gt;Stock:1000tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Belawai"
    />
    <marker lat = "2.50686" lng = "112.316" 
    html = 'Fish name:ikan gelama/tengkerong&lt;br&gt;Stock:289.35158tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Mukah"
    />
    <marker lat = "2.225" lng = "111.335" 
    html = 'Fish name:ikan kerisi&lt;br&gt;Stock:5831.09568tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Tanjung Manis"
    />
    <marker lat = "3.19783" lng = "113.091" 
    html = 'Fish name:ikan lolong&lt;br&gt;Stock:3735.66249tan metrik&lt;br&gt;Fish length8cm&lt;br&gt;Status:matured&lt;br&gt;
    label = "LKIM Bintulu"
    />
    <marker lat = "4.33737" lng = "113.995" 
    html = 'Fish name:ikan pelaling/temenong&lt;br&gt;Stock:34.42008tan metrik&lt;br&gt;Fish length13cm&lt;br&gt;Status:matured&lt;br&gt;
    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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜