В моем приложении у меня есть XML-файл, и я хочу проанализировать XML-файл и извлечь данные из тегов XML. Вот мой XML-файл.Получение дочерних узлов из XML
<?xml version='1.0' encoding='utf-8'?>
<OrdersEnquiryResponse Message="" >
<TableDetails Outlet="S2" Date="01/01/15" Time="12:19:47" TableNo=" 1" Covers="2" Waiter="DD " KOTs=" 4981"/>
<Items>
<Item Code="IBMCF" Name="BRANDY-MC.NO.1-750ML" Qty=" 1.000" Instructions="" Status="F"/>
<Item Code="IBMHF" Name="BRANDY-MANSION HOUSE-750ML" Qty=" 1.000" Instructions="" Status="F"/>
<Item Code="IBCRF" Name="BRANDY-CAESAR-750ML" Qty=" 1.000" Instructions="" Status="P"/>
<Item Code="IBLVF" Name="BRANDY�LOUIS VERNANT�750ML" Qty=" 1.000" Instructions="" Status="P"/>
</Items>
<TableDetails Outlet="S2" Date="01/01/15" Time="12:19:47" TableNo=" 4" Covers="2" Waiter="DD " KOTs=" 4982"/>
<Items>
<Item Code="IBMRF" Name="BRANDY�MORPHEUS-XO-PREMIUM-750ML" Qty=" 1.000" Instructions="" Status="F"/>
<Item Code="IBMCF" Name="BRANDY-MC.NO.1-750ML" Qty=" 1.000" Instructions="" Status="F"/>
<Item Code="IBCRF" Name="BRANDY-CAESAR-750ML" Qty=" 1.000" Instructions="" Status="P"/>
<Item Code="IBLVF" Name="BRANDY�LOUIS VERNANT�750ML" Qty=" 1.000" Instructions="" Status="P"/>
</Items>
</OrdersEnquiryResponse>
Я использовал парсер DOM для анализа вышеуказанного XML-файла.
Document doc = parser.getDomElement(xml);
NodeList nlll = doc.getElementsByTagName("TableDetails");
int category_len = nlll.getLength();// length of Table Details
Log.d("LENGTH 0", category_len + "");
for (int i = 0; i < nlll.getLength(); i++) {
Node nNode = nlll.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
outlet = eElement.getAttribute(KEY_OUTLET);
order_date = eElement.getAttribute(KEY_DATE);
order_time = eElement.getAttribute(KEY_TIME);
tbno = eElement.getAttribute(KEY_TNO);
waiter = eElement.getAttribute(KEY_WAITOR);
kotNo = eElement.getAttribute(KEY_KOT);
no_of_coverss = eElement.getAttribute(KEY_COVER);
System.out.println(order_date);
System.out.println(order_time);
NodeList nl = doc.getElementsByTagName("Item");
int category_len1 = nl.getLength();// length of Item
Log.d("LENGTH 1", category_len1 + "");
column1 = new String[category_len1];
column2 = new String[category_len1];
column3 = new String[category_len1];
column4 = new String[category_len1];
column5 = new String[category_len1];
column6 = new String[category_len1];
for (int j = 0; j < nl.getLength(); j++) {
Node nNode1 = nl.item(i);
if (nNode1.getNodeType() == Node.ELEMENT_NODE) {
Element eElement1 = (Element) nNode1;
column4[i] = (i + 01) + "";
column5[i] = eElement1.getAttribute(KEY_CODE);
column1[i] = eElement1.getAttribute(KEY_NAME);
column2[i] = eElement1.getAttribute(KEY_QUANTITY);
column3[i] = eElement1.getAttribute(KEY_INSTRUCTION);
column6[i] = eElement1.getAttribute(KEY_STATUS);
}
}
}
}
Всякий раз, когда я получаю Item тег NodeList nl = doc.getElementsByTagName("Item");
я получаю все <Item>
тег. Мне нужны только первые четыре тега <Item>
, когда я получаю первый тег <TableDetails>
. Как это можно сделать возможным. Заранее спасибо.