Я привел образец XML-файла, который я использую, чтобы получить список продуктов и накачать его в представление списка. В то время как я пытаюсь добавить элемент в свой XML-файл во время выполнения, я получаю URL-адрес с искаженным исключением. путь, который я указываю, недействителен. Я поместил свой xml в файл package / res / raw / testcalory.xml.kindly.
ИСКЛЮЧЕНИЕ: 12-26 14: 35: 58.516: WARN / System.err (11597): java.net.MalformedURLException: протокол не найден: testcalory.xml
//sample xml
<?xml version="1.0"?>
<company>
<Row>
<Item_Name>Aam Ras </Item_Name>
<CalorieContentPerGram>1.12</CalorieContentPerGram>
</Row>
<Row>
<Item_Name>Akuri </Item_Name>
<CalorieContentPerGram>1.62</CalorieContentPerGram>
</Row>
<Row>
<Item_Name>Almond ICC </Item_Name>
<CalorieContentPerGram>5.35</CalorieContentPerGram>
</Row>
<Row>
<Item_Name>Almond Milkshake </Item_Name>
<CalorieContentPerGram>1.16</CalorieContentPerGram>
</Row>
</company>
//sample code
private void addItem() {
// TODO Auto-generated method stub
try{
FileOutputStream fOut = openFileOutput("testcalory", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
String filepath = "testcalory.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
org.w3c.dom.Element Row;
org.w3c.dom.Element Item_Name;
org.w3c.dom.Element CalorieContentPerGram;
Node company = doc.getElementsByTagName("company").item(0);
Row=doc.createElement("Row");
Item_Name=doc.createElement("Item_Name");
Item_Name.appendChild(doc.createTextNode("hulala"));
Row.appendChild(Item_Name);
CalorieContentPerGram=doc.createElement("CalorieContentPerGram");
CalorieContentPerGram.appendChild(doc.createTextNode("556"));
Row.appendChild(CalorieContentPerGram);
company.appendChild(Row);
//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));
transformer.transform(source, result);
result.setWriter(osw);
osw.flush();
osw.close();
}catch(ParserConfigurationException pce){
pce.printStackTrace();
}catch(TransformerException tfe){
tfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(SAXException sae){
sae.printStackTrace();
}
}