私はそれが見えるXMLを持っています
<root status="Y">
<mb mbcode="150201" mbname="AKASH KUNDU" branchid="1" pwd="admin"/>
</root>
私はそれを解析してすべてのデータを個別に取得したいのですが、多くの例を見ましたが、単一の XML がこのように見えるわけではありません。
<md>
<mbcode>150201</mbcode>
<mbname>AKASH KUNDU</mbname>
</md>
この種の XML を解析する方法を知っています。この種の XML 解析を行う方法を教えてください。
私はこれまでやってきました
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = new ByteArrayInputStream(responseBody.getBytes("UTF-8"));
Document doc = db.parse(is);
NodeList words=doc.getElementsByTagName("root");
for (int i=0;i<words.getLength();i++) {
Toast.makeText( getApplicationContext()," " + ((Element)words.item(i)).getAttribute("value"),Toast.LENGTH_SHORT).show();
}
私の解決策を見つけました
//getting the xml Value as per child node form the saved xml
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = new ByteArrayInputStream(readString.getBytes("UTF-8"));
Document doc = db.parse(is);
NodeList root=doc.getElementsByTagName("root");
for (int i=0;i<root.getLength();i++) {
loginStatus = "" + ((Element)root.item(i)).getAttribute("status");
}
if(loginStatus.equalsIgnoreCase("Y"))
{
NodeList mb=doc.getElementsByTagName("mb");
for (int i=0;i<mb.getLength();i++) {
setMbcode("" + ((Element)mb.item(i)).getAttribute("mbcode"));
setMbname("" + ((Element)mb.item(i)).getAttribute("mbname"));
branchid = "" + ((Element)mb.item(i)).getAttribute("branchid");
pwd = "" + ((Element)mb.item(i)).getAttribute("pwd");
}