Ngoài những gì đã được nói, tùy thuộc vào việc bạn sử dụng XML, mã của bạn có khả năng không chính xác vì nó hủy bỏ kết thúc dòng. Ví dụ, đoạn mã này:
package temp.stackoverflow.q15849706;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import com.thoughtworks.xstream.XStream;
public class ReadXmlLines {
public String read1(BufferedReader br) throws IOException {
try {
String s = "";
String tempString;
int i = 0;
while ((tempString = br.readLine()) != null) {
s = s.concat(tempString);
// s=s+tempString;
i = i + 1;
if (i % 1000 == 0) {
System.out.println(Integer.toString(i));
}
}
return s;
} finally {
br.close();
}
}
public static void main(String[] args) throws IOException {
ReadXmlLines r = new ReadXmlLines();
URL url = ReadXmlLines.class.getResource("xml.xml");
String xmlStr = r.read1(new BufferedReader(new InputStreamReader(url
.openStream())));
Object ob = null;
XStream xs = new XStream();
xs.alias("root", Root.class);
// This is incorrectly read/parsed, as the line endings are not
// preserved.
System.out.println("----------1");
System.out.println(xmlStr);
ob = xs.fromXML(xmlStr);
System.out.println(ob);
// This is correctly read/parsed, when passing in the URL directly
ob = xs.fromXML(url);
System.out.println("----------2");
System.out.println(ob);
// This is correctly read/parsed, when passing in the InputStream
// directly
ob = xs.fromXML(url.openStream());
System.out.println("----------3");
System.out.println(ob);
}
public static class Root {
public String script;
public String toString() {
return script;
}
}
}
và tập tin xml.xml này trên classpath (trong gói giống như lớp):
<root>
<script>
<![CDATA[
// taken from http://www.w3schools.com/xml/xml_cdata.asp
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1;
}
else
{
return 0;
}
}
]]>
</script>
</root>
xuất ra như sau. Hai dòng đầu tiên cho thấy kết thúc dòng đã bị loại bỏ, và do đó làm cho Javascript trong phần CDATA không hợp lệ (như nhận xét JS đầu tiên bây giờ nhận xét ra toàn bộ JS, vì các dòng JS đã được hợp nhất).
----------1
<root> <script><![CDATA[// taken from http://www.w3schools.com/xml/xml_cdata.aspfunction matchwo(a,b){if (a < b && a < 0) then { return 1; }else { return 0; }}]]> </script></root>
// taken from http://www.w3schools.com/xml/xml_cdata.aspfunction matchwo(a,b){if (a < b && a < 0) then { return 1; }else { return 0; }}
----------2
// taken from http://www.w3schools.com/xml/xml_cdata.asp
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1;
}
else
{
return 0;
}
}
...
Nguồn
2013-04-06 11:25:30
Mục đích của bạn là phân tích cú pháp tệp này? Tại sao không chỉ tải nó bằng Xerces/SAX/công cụ phân tích cú pháp khác? –
Chuỗi '+' và 'concat' rất không hiệu quả nếu các chuỗi là lớn. Sử dụng 'StringBuilder' hoặc chuyển trực tiếp' InputStream'/'Reader' tới trình phân tích cú pháp xml. –
Hoặc nếu bạn thực sự cần dòng, hãy sử dụng một cái gì đó như thế này - http://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/IOUtils.html#readLines%28java .io.Reader% 29. –