有如下的XML字符串:
<?xml version="1.0" encoding="UTF-8"?> <?Label SINFON|RATE|328|SUCCESS ?> <RateHeader xmlns="rate.fidelio.2.0" rateStatus="ACTIVE" yieldStatus="NONE"> <hotelCode>SINFON</hotelCode> <chainCode>COL</chainCode> <rateClass/> <RateCategory> <catDescription>Rack rates</catDescription> <category>RAK</category> </RateCategory> <rateCode>WHL01</rateCode> <startSell>2013-12-02</startSell> <endSell>2015-01-22</endSell> <advancedBookingDays>2</advancedBookingDays> <displayText>Tour Series</displayText> <folioText>Room Charge</folioText> <shortInfo>tour series/min. 5 tours</shortInfo> <minLos>3</minLos> <maxLos>20</maxLos> <rhythm>W</rhythm> <packages>BRF</packages> <packages>IBF</packages> <marketCode>RAC</marketCode> <sourceCode>COL</sourceCode> <commission>0</commission> <foreignCurrencyDepartmentCode/> <departmentCode>100</departmentCode> <weekendDepartmentCode>100</weekendDepartmentCode> <rateBucket>LOW</rateBucket> </RateHeader>
要求从中读取rateCode节点的值,实现方法如下:
public static String getRateCodeFromXmlString(String xmlString) { String rateCodeMessage=""; StringReader read = new StringReader(xmlString); //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入 InputSource source = new InputSource(read); //创建一个新的SAXBuilder SAXBuilder saxbBuilder = new SAXBuilder(); try { //通过输入源构造一个Document Document doc = saxbBuilder.build(source); //取的根元素 Element root = doc.getRootElement(); List<?> node = root.getChildren(); for (int i = 0; i < node.size();i++) { Element element=(Element)node.get(i); if(element.getName().equals("rateCode")){ rateCodeMessage=element.getText(); } } return rateCodeMessage; } catch (JDOMException e) { e.printStackTrace(); return ""; } catch (IOException e) { e.printStackTrace(); return ""; } }
有如下XML字符串:
<?xml version="1.0" encoding="UTF-8"?> <?Label EXPSD|RTAV|10000281|SUCCESS?> <RtavMessage xmlns="rtav.fidelio.1.0"> <HotelReference hotelCode="EXPSD"/> <DailyInventories> <DailyInventory datum="2014-08-10"> <RoomTypeInventories> <RoomTypeInventory roomType="1KESS" generic="0" physicalRooms="22" roomTypeOverbook="0" outOfOrder="6"> <Tentative count="0" blockCount="0" resvCount="0"/> <Definite count="6" blockCount="0" resvCount="6"/> </RoomTypeInventory> <RoomTypeInventory roomType="1KSTS" generic="0" physicalRooms="81" roomTypeOverbook="0" outOfOrder="16"> <Tentative count="0" blockCount="0" resvCount="0"/> <Definite count="14" blockCount="0" resvCount="14"/> </RoomTypeInventory> <RoomTypeInventory roomType="2BSVS" generic="0" physicalRooms="34" roomTypeOverbook="0" outOfOrder="6"> <Tentative count="0" blockCount="0" resvCount="0"/> <Definite count="12" blockCount="0" resvCount="12"/> </RoomTypeInventory> </RoomTypeInventories> </DailyInventory> <DailyInventory datum="2014-08-11"> <RoomTypeInventories> <RoomTypeInventory roomType="1KSTS" generic="0" physicalRooms="81" roomTypeOverbook="0" outOfOrder="16"> <Tentative count="0" blockCount="0" resvCount="0"/> <Definite count="14" blockCount="1" resvCount="13"/> </RoomTypeInventory> </RoomTypeInventories> </DailyInventory> </DailyInventories> </RtavMessage>
要求获取roomType属性的值,如果有多个,则以空格间隔,不获取重复的值。实现代码如下:
public static String getRoomTypeFromXmlString(String xmlString) { String roomTypeMessage=""; StringReader read = new StringReader(xmlString); //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入 InputSource source = new InputSource(read); //创建一个新的SAXBuilder SAXBuilder saxbBuilder = new SAXBuilder(); try { //通过输入源构造一个Document Document doc = saxbBuilder.build(source); //取的根元素 Element root = doc.getRootElement(); List<?> node = root.getChildren(); for (int i = 0; i < node.size();i++) { Element element=(Element)node.get(i); List<?> subNode = element.getChildren(); for(int j=0;j<subNode.size();j++) { Element subElement=(Element)subNode.get(j); List<?> subNode2 = subElement.getChildren(); for(int k=0;k<subNode2.size();k++) { Element subElement2=(Element)subNode2.get(k); List<?> subNode3 = subElement2.getChildren(); for(int m=0;m<subNode3.size();m++) { Element subElement3=(Element)subNode3.get(m); String temp=subElement3.getAttributeValue("roomType"); String temp2=roomTypeMessage.indexOf(temp)>=0?"":(temp+" "); roomTypeMessage+=temp2; } } } } return roomTypeMessage; } catch (JDOMException e) { e.printStackTrace(); return ""; } catch (IOException e) { e.printStackTrace(); return ""; } }原文链接:https://www.f2er.com/xml/296156.html