0
내 응용 프로그램에서 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>
위의 XML 파일을 구문 분석하기 위해 DOM 구문 분석기를 사용했습니다.
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);
}
}
}
}
가 나는 항목 태그 NodeList nl = doc.getElementsByTagName("Item");
나는 모든 <Item>
태그를 얻고을받을 때마다. 나는 처음 태그를 얻었을 때 처음 네 개의 <Item>
태그 만 필요합니다. 어떻게 이것이 가능하게 할 수 있습니다. 미리 감사드립니다.