dom4j解析xml(自记)

<?xml version="1.0" encoding="UTF-8"?>
<getSubScriptionListReturn>
	<subScriptionInfos>
		<!--Zero or more repetitions: -->
		<item>
			<parentName>
				<!--Zero or more repetitions: -->
				<item>
					<name>zhangsan1</name>
					<value>20</value>
					<langInfo>
						<language>zh1</language>
						<country>CN1</country>
					</langInfo>
				</item>
			</parentName>
			<productName>
				<!--Zero or more repetitions: -->
				<item>
					<name>Zhangsan2</name>
					<value>21</value>
					<langInfo>
						<language>zh2</language>
						<country>CN2</country>
					</langInfo>
				</item>
			</productName>
			<spID>10</spID>
			<subScriptionState>01</subScriptionState>
			<isAutoExtend>zz</isAutoExtend>
			<parentID>12</parentID>
			<userID>12</userID>
			<productDesc>
				<!--Zero or more repetitions: -->
				<item>
					<name>lisi</name>
					<value>20</value>
					<langInfo>
						<language>en</language>
						<country>US</country>
					</langInfo>
				</item>
			</productDesc>
			<unSubTime>2010-10-20 10:10:10</unSubTime>
			<subTime>2010-10-20 10:10:10</subTime>
			<parentType>12</parentType>
			<productID>12</productID>
			<extensionInfo>
				<namedParameters>
					<!--Zero or more repetitions: -->
					<item>
						<key>totalAmount</key>
						<value>150</value>
					</item>


					<item>
						<key>totalAmount2</key>
						<value>150</value>
					</item>
					<item>
						<key>totalAmount3</key>
						<value>150</value>
					</item>
				</namedParameters>
			</extensionInfo>
			<spName>
				<!--Zero or more repetitions: -->
				<item>
					<name>eee</name>
					<value>wwww</value>
					<langInfo>
						<language>eee</language>
						<country>fre</country>
					</langInfo>
				</item>
			</spName>
			<nextChargeTime>2010-10-12 10:20:30</nextChargeTime>


		</item>
	</subScriptionInfos>
</getSubScriptionListReturn>

 

 

JSONObject jsonObject=new JSONObject();
				Document dom = DocumentHelper.parseText(xml);
				Element root = dom.getRootElement();
				
				String key=root.element("subScriptionInfos").element("item").element("nextChargeTime").getName();
				String value=root.element("subScriptionInfos").element("item").element("nextChargeTime").getText();
				//time
				jsonObject.put(key, value);
				
				Element items=root.element("subScriptionInfos").element("item").element("extensionInfo").element("namedParameters");

				int i;
				int size=items.elements().size();
				for(i=0;i<size;i++){
					Element item=(Element)items.elements().get(i);
					String totalAmountElmentText=item.element("key").getText();
					if(totalAmountElmentText.equals("totalAmount")){
						
						jsonObject.put(totalAmountElmentText, item.element("value").getText());
						break;
					}
					
				}

 

 

//我只要得到红色部分的东东

你可能感兴趣的:(dom4j)