在android应用程序中,我将数据存储到xml文件中。我正在使用DOM方法在xml文件中添加新标签。有时它会添加一些垃圾标签,文件不再使用。这种行为的原因是什么?我读到DOM对于大尺寸文件来说很慢。最大文件大小约为800KB。是这个问题的原因吗? 你能提供解决方案来避免这个问题吗?
<?xml version="1.0" encoding="UTF-8"?>
<MainCategory>
<Type>Nos</Type>
<Category>Briyani</Category>
<CurrentStock>0.00</CurrentStock>
<Purchased>
<Items>
<Date>01/12/2016</Date>
<Quantity>116.00</Quantity>
<ManualUpdate>Yes</ManualUpdate>
<SourceDetails>NIL</SourceDetails>
<Reason>None</Reason>
</Items>
</Purchased>
</MainCategory>
文件格式如上所示。购买的物品反复添加。 但有时候,作为垃圾和文件添加的结束标记( nCategory&gt; )中的部分字符串无用,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<MainCategory>
<Type>Nos</Type>
<Category>Briyani</Category>
<CurrentStock>0.00</CurrentStock>
<Purchased>
<Items>
<Date>01/12/2016</Date>
<Quantity>116.00</Quantity>
<ManualUpdate>Yes</ManualUpdate>
<SourceDetails>NIL</SourceDetails>
<Reason>None</Reason>
</Items>
</Purchased>
</MainCategory>nCategory>
添加新标记代码如下:
try{
String filepath = ctx.getFilesDir().getAbsolutePath()+"/"+XmlfileName;
File inputFile = new File(filepath);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(inputFile);
// Get the root element
Node InvItems = doc.getFirstChild();
NodeList list = InvItems.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
//Update Current Stock
if ("CurrentStock".equals(node.getNodeName())) {
node.setTextContent(CurrentStock);
}
if (PurOrUsedStr.equals(node.getNodeName())) {
// append a new node to staff
Element p1 = doc.createElement("Items");
Element date = doc.createElement("Date");
date.appendChild(doc.createTextNode(DateStr));
p1.appendChild(date);
Element quantity = doc.createElement("Quantity");
quantity.appendChild(doc.createTextNode(UpdateQuantity));
p1.appendChild(quantity);
Element AutoOrManual = doc.createElement("ManualUpdate");
AutoOrManual.appendChild(doc.createTextNode(AutoManuUpdate));
p1.appendChild(AutoOrManual);
Element SrcDetails = doc.createElement("SourceDetails");
SrcDetails.appendChild(doc.createTextNode(SourceDetails));
p1.appendChild(SrcDetails);
Element ReasonSplCmt = doc.createElement("Reason");
ReasonSplCmt.appendChild(doc.createTextNode(ReasonStr));
p1.appendChild(ReasonSplCmt);
Element UpdateTime = doc.createElement("UpdateTime");
UpdateTime.appendChild(doc.createTextNode(UpdateTimeStr));
p1.appendChild(UpdateTime);
node.appendChild(p1);
IsPurOrUsedTagAvail = true;
}
}
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
//StreamResult result = new StreamResult(new File(filepath));
StreamResult result = new StreamResult(new FileOutputStream(filepath));
transformer.transform(source, result);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
StatusStr = "Exception";
}catch (TransformerException tfe) {
StatusStr = "Exception";
tfe.printStackTrace();
} catch (IOException ioe) {
StatusStr = "Exception";
ioe.printStackTrace();
} catch (SAXException sae) {
StatusStr = "Exception";
sae.printStackTrace();
}