public class xml2Instance {
public static void main(String[] args){
SAXReader reader = new SAXReader();
try {
Document doc = reader.read
(new FileInputStream("C:/Users/Administrator/Desktop/first.xml"));
Element root = doc.getRootElement();
Customer customer = new Customer();
System.out.println(root.attributeValue("CustName"));
customer.setCustName(root.attributeValue("CustName"));
customer.setType(root.attributeValue("Type"));
customer.setDest(root.attributeValue("Dest"));
customer.setFlight(root.attributeValue("Flight"));
customer.setTime(root.attributeValue("Time"));
System.out.println(customer);
}
}
heroes , and why I can not read file values , um, I was just using dom4j first.xml written , but it is there ah
which also display the content, for the thing at the time to read it will show null
?
------ Solution ----------------------------------- ---------
xml file you do not know what kind , but feel you should have no access to the outer Customer (); object
give you one example
public Map<String, String> getProvinceMap() {
Map<String, String> proMap = new HashMap<String, String>();
try {
InputStream in = DBHelper.getDBHelper().getContext().getResources()
.getAssets().open("province.xml");
InputStream is = IOHelper.fromInputStreamToInputStreamInCharset(in,"utf-8");
SAXReader sr = new SAXReader();// 获取读取xml的对象。
Document document = sr.read(is);
Element root = document.getRootElement();
List<?> elementlist = root.elements("city");
for (Object obj : elementlist) {
Element row = (Element) obj;
String quName = row.attribute("quName").getText();
String pyName = row.attribute("pyName").getText();
proMap.put(quName, pyName);
}
} catch (Exception e) {
Log.e("tag", "read configure error");
}
return proMap;
}
<?xml version="1.0" encoding="utf-8"?>
<china dn="day">
<city quName="黑龙江" pyName="heilongjiang" cityname="哈尔滨" state1="21" state2="7" stateDetailed="小到中雨转小雨" tem1="21" tem2="13" windState="东风4-5级转3-4级"/>
<city quName="吉林" pyName="jilin" cityname="长春" state1="8" state2="9" stateDetailed="中雨转大雨" tem1="19" tem2="14" windState="东南风3-4级"/>
<city quName="辽宁" pyName="liaoning" cityname="沈阳" state1="8" state2="9" stateDetailed="中雨转大雨" tem1="21" tem2="18" windState="东南风小于3级转3-4级"/>
</china>
------ For reference only ----------------------------------- ----
look first.xml content .
没有评论:
发表评论