我有一个看起来像的 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");
}