我有文件xml并且是非常大的12k行,我想将这个xml解析为JSON。我做了这个,但它切割和Json小姐不到xml。我认为这发生了 字符串的字符长度有限,超出了该值。我不知道如何正常工作。
private InputStream readXMLFile() {
String path = Environment.getExternalStorageDirectory() + File.separator + "yourFolder";
File folder = new File(path);
if (!folder.exists())
folder.mkdir();
File file = new File(folder, "RM_7.xml");
Log.e("exist" , file.exists() + " ");
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
byte[] data = read(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inputStream;
}
public byte [] read(文件文件)抛出IOException {
ByteArrayOutputStream ous = null;
InputStream ios = null;
byte[] buffer;
try {
buffer = new byte[4096];
ous = new ByteArrayOutputStream();
ios = new FileInputStream(file);
int read = 0;
while ((read = ios.read(buffer)) != -1) {
ous.write(buffer, 0, read);
}
}finally {
try {
if (ous != null)
ous.close();
} catch (IOException e) {
}
try {
if (ios != null)
ios.close();
} catch (IOException e) {
}
}
InputStream inputStream = ios;
String xml = IOUtils.toString(inputStream);
JSON objJson = new XMLSerializer().read(xml);
writeToFile(objJson.toString());
try {
JSONObject jsonObj = XML.toJSONObject(new String(buffer,"utf-8"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ous.toByteArray();
}
这是我的xml文件:
<?xml version="1.0"?>
<root>
<DICTIONARIES>
<BIN_TYPES>
<BIN_TYPE id="40259091" description="POJ_1100" />
<BIN_TYPE id="400181537" description="POJ_2500" />
<BIN_TYPE id="651131896" description="POJ_7000" />
<BIN_TYPE id="798636566" description="POJ_120" />
<BIN_TYPE id="1320806807" description="POJ_660" />
<BIN_TYPE id="1533220545" description="POJ_60" />
<BIN_TYPE id="2072388553" description="POJ_240" />
<BIN_TYPE id="2361832307" description="POJ_5000" />
<BIN_TYPE id="3319806799" description="POJ_80" />
<BIN_TYPE id="3369836500" description="POJ_10000" />
</BIN_TYPES>
<TRASH_TYPES>
<TRASH_TYPE id="497" description="Zm" />
<TRASH_TYPE id="498" description="pet" />
<TRASH_TYPE id="499" description="szkło" />
<TRASH_TYPE id="500" description="ziel" />
</TRASH_TYPES>
<BUILDING_TYPES>
<BUILDING_TYPE id="91" description="ZAB_WIEL" />
<BUILDING_TYPE id="93" description="DOM_JEDN" />
<BUILDING_TYPE id="94" description="DZIAL_GAST" />
<BUILDING_TYPE id="95" description="DZIAL_HAND" />
<BUILDING_TYPE id="96" description="DZIAL_PROD" />
<BUILDING_TYPE id="97" description="DZIAL_USLU" />
<BUILDING_TYPE id="98" description="INNE" />
<BUILDING_TYPE id="99" description="INNE1" />
<BUILDING_TYPE id="100" description="NIEZIDENTYFIKOWANA" />
<BUILDING_TYPE id="102" description="OGR_DZIALK" />
<BUILDING_TYPE id="103" description="PRZED_ZLOB" />
<BUILDING_TYPE id="104" description="PRZYCHOD" />
<BUILDING_TYPE id="105" description="SZKOLA" />
<BUILDING_TYPE id="107" description="SZPITAL" />
<BUILDING_TYPE id="109" description="ZAB_JEDN" />
<BUILDING_TYPE id="110" description="ZAB_WIEL1" />
<BUILDING_TYPE id="111" description="KAMIEN" />
<BUILDING_TYPE id="112" description="SKLEP1" />
</BUILDING_TYPES>
<SEGREGATION_TYPES>
<SEGREGATION_TYPE id="2" description="N" />
<SEGREGATION_TYPE id="3" description="T" />
</SEGREGATION_TYPES>
</DICTIONARIES>
</root>
答案 0 :(得分:0)
您应该使用XmlPullParser,因为您说在一个String
中存储12k行可能会受到限制。
以下是文档中的示例:
public class SimpleXmlPullApp
{
public static void main (String args[])
throws XmlPullParserException, IOException
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("Text "+xpp.getText());
}
eventType = xpp.next();
}
System.out.println("End document");
}
}
开发人员指南有关于如何使用它的分步说明。 See here