package com.vsked.test.string;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class TestReadFromString {

	public static void main(String[] args) throws Exception {
//		testReadFromString1();
		testReadFromString2();

	}
	
	public static void testReadFromString1(){
		String xmlData="";
		xmlData+="<root>";
		xmlData+="<house>";
		xmlData+="<door>hello door</door>";
		xmlData+="</house>";
		xmlData+="";
		xmlData+="";
		xmlData+="<info>this is myinfo</info>";
		xmlData+="</root>";
		
		System.out.println(readXmlA1(xmlData));
	}
	
	public static void testReadFromString2() throws Exception{
		String xmlData="";
		xmlData+="<productList>";
		for(int i=0;i<10;i++){
		xmlData+="<product>";
		xmlData+="<productName>p"+i+"</productName>";
		xmlData+="<productCode>c"+i+"</productCode>";
		xmlData+="</product>";
		}
		xmlData+="</productList>";
		System.out.println(getSomeData(xmlData));
	}
	
	public static Map<String, String> readXmlA1(String xml) {
		Document doc = null;
		Map<String, String> map = new HashMap<String, String>();
		try {
			doc = DocumentHelper.parseText(xml); // 将字符串转为XML
			Element rootElt = doc.getRootElement(); // 获取根节点
			String info = rootElt.elementTextTrim("info");
			Element recordEle1 = rootElt.element("house");
			String door = recordEle1.elementTextTrim("door");
			map.put("info", info);
			map.put("door", door);
			String window = rootElt.elementTextTrim("window");
			map.put("window", window);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	public static Map<String,String> getSomeData(String xml) throws Exception{
		Map<String, String> rm=new HashMap<String, String>();
		Document doc = DocumentHelper.parseText(xml);
		Element rootElt = doc.getRootElement(); 
		Iterator<?> it = rootElt.elementIterator();
		while (it.hasNext()) {
			Element recordEle = (Element) it.next();
			System.out.println(recordEle);
			
		}
		
		return rm;
	}

}

@@ -86,32 +86,8 @@
 r;%0D%0A
-import java.util.List;%0D%0A
 impo
@@ -105,18 +105,16 @@
 l.Map;%0D%0A
-%0D%0A
 import o
@@ -836,24 +836,39 @@
 a+=%22%3Cproduct
+ ps=%5C%22ps%22+i+%22%5C%22
 %3E%22;%0D%0A%09%09xmlDa
@@ -1731,22 +1731,35 @@
 %3CString,
+Map%3C
 String
+, String%3E
 %3E getSom
@@ -1809,22 +1809,35 @@
 String, 
+Map%3C
 String
+, String%3E
 %3E rm=new
@@ -1845,38 +1845,50 @@
 HashMap%3CString, 
+Map%3C
 String
+,String%3E
 %3E();%0D%0A%09%09Document
@@ -2084,24 +2084,226 @@
 it.next();%0D%0A
+%09%09%09Map%3CString, String%3E tmpMap=new HashMap%3CString, String%3E();%0D%0A%09%09%09tmpMap.put(%22productName%22, recordEle.elementText(%22productName%22));%0D%0A%09%09%09tmpMap.put(%22productCode%22, recordEle.elementText(%22productCode%22));%0D%0A//
 %09%09%09System.ou
@@ -2321,23 +2321,196 @@
 ecordEle
+.attributeValue(%22ps%22)
 );%0D%0A
+//
 %09%09%09
+System.out.println(recordEle.elementText(%22productName%22)+%22%7C%22+recordEle.elementText(%22productCode%22));%0D%0A%09%09%09rm.put(recordEle.attributeValue(%22ps%22), tmpMap);
 %0D%0A%09%09%7D%0D%0A%09

update the read from xml for