Java教程

java实现xml文件保存与读取

本文主要是介绍java实现xml文件保存与读取,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  

File GGCNimages_update1XMLFile = FileUtil.isExistXmlFile("http://www.baidu.com/sitemap.xml", getContext());
if (GGCNimages_update1XMLFile != null) {
Log.d("debug", "从本地缓存文件中加载轮播图片");
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(GGCNimages_update1XMLFile);
Element element = document.getDocumentElement();
NodeList nodeList = element.getElementsByTagName("image");
List<BannerViewItem> bannerViewItemList1 = new ArrayList<BannerViewItem>();
for (int i = 0; i < nodeList.getLength(); i ++) {
Element areaElement = (Element) nodeList.item(i);
String imageurl = areaElement.getElementsByTagName("imageurl").item(0).getTextContent();
String h5url = areaElement.getElementsByTagName("h5url").item(0).getTextContent();
BannerViewItem bannerViewItem = new BannerViewItem();
bannerViewItem.setImageUrl(imageurl);
bannerViewItem.setH5Url(h5url);
bannerViewItemList1.add(bannerViewItem);
}
initBannerView(bannerViewItemList1);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


//------------------------------------------------------------------------------------------------------------------------------


String filePath = getContext().getDir("xml", Context.MODE_PRIVATE).getAbsolutePath() + File.separator + "sitemap.xml";
File GGCNimages_update1XMLFile = new File(filePath);
if (GGCNimages_update1XMLFile.exists()) {
boolean resultOfDelete = GGCNimages_update1XMLFile.delete();
Log.d("debug", "InternationalRepPageFragment中删除轮播图文件返回的结果->" + resultOfDelete);
}
InputStream inputStream = new ByteArrayInputStream(callBackStr.getBytes());
try {
OutputStream outputStream = new FileOutputStream(GGCNimages_update1XMLFile);
int ascii = inputStream.read();
while(ascii != -1) {
outputStream.write(ascii);
ascii = inputStream.read();
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

这篇关于java实现xml文件保存与读取的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!