2017年3月29日 星期三

Android how to parse the XML

XML Content


/*
<car>
<vacancyinfo capacity="327" vacancy="145"/>
</car>*/



parse Program

xml = connection(SharedUtils.CAR_VACANCY_URL, null);

InputStream inputStream = new    ByteArrayInputStream(xml.getBytes());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputStream);
doc.getDocumentElement().normalize();
NodeList vacancyinfoNodeLis = doc.getElementsByTagName("vacancyinfo");
Element vacancyinfo = (Element) vacancyinfoNodeLis.item(0);
String capacity=vacancyinfo.getAttribute("capacity");
String vacancy=vacancyinfo.getAttribute("vacancy");