自己写的:
1 保证null时,或是异常时,也要保存成默认值,如字符串是"",int是0
2 getNodeValue有throw异常,无需要往上级throw直接压掉;
所以代码写起来很长一节.有点郁闷.
---------
private void parseUserInfo(boolean flush) throws Exception {
//把一级的节点键值放到对应成员变量上
if (! flush && (this.reader.getUserInfo() != null) ){
return ;//已获取,不必更新
}
Element node = new Api(_this.reader).get_user_info();
if ( ! "member".equalsIgnoreCase(node.getTagName())){
throw new Exception("提取会员资料出错:xml内容不包含会员资料,或格式已变更");
}
Hashtable userInfo = new Hashtable();
NodeList list = node.getElementsByTagName("userID");
try{
if (list.getLength() > 0){
int uid = 0;
try{
uid = Integer.parseInt(list.item(0).getFirstChild().getNodeValue());
}catch( Exception e){
System.out.println("tip>parseUserInfo uid " + e);
uid = 0;
}
userInfo.put("id", new Integer(uid));
}
list = node.getElementsByTagName("username");
if (list.getLength() > 0){
String temp = null;
try{
temp = list.item(0).getFirstChild().getNodeValue();
}catch( Exception e){
System.out.println("tip>parseUserInfo username " + e);
}finally{
if (null == temp) temp = "";
}
userInfo.put("name", temp);
}
list = node.getElementsByTagName("realName");
if (list.getLength() > 0){
String temp = null;
try{
temp = list.item(0).getFirstChild().getNodeValue();
}catch( Exception e){
System.out.println("tip>parseUserInfo username " + e);
}finally{
if (null == temp) temp = "";
}
userInfo.put("real", temp );
}
list = node.getElementsByTagName("imoney");
if (list.getLength() > 0){
float temp = 0;
try{
temp = Float.parseFloat(list.item(0).getFirstChild().getNodeValue());
}catch(Exception e){
System.out.println("tip>parseUserInfo imoney " + e);
temp = 0;
}
userInfo.put("money", new Float(temp ) );
}
list = node.getElementsByTagName("isAuthor");
if (list.getLength() > 0){
boolean temp = false;
try{
temp = list.item(0).getFirstChild().getNodeValue().trim().equals("0");
}catch(Exception e){
System.out.println("tip>parseUserInfo isAuthor " + e);
}
userInfo.put("isAuth", new Boolean(temp) );
}
list = node.getElementsByTagName("userNumber");
if (list.getLength() > 0){
String temp = null;
try{
temp = list.item(0).getFirstChild().getNodeValue();
}catch(Exception e){
System.out.println("tip>parseUserInfo userNumber " + e);
}finally{
if (null == temp) temp = "";
}
userInfo.put("no.", temp);
}
list = node.getElementsByTagName("gender");
if (list.getLength() > 0){
String temp = "保密";
try{
if (list.item(0).getFirstChild().getNodeValue().trim().equals("1")){
temp = "男";
}else if (list.item(0).getFirstChild().getNodeValue().trim().equals("1")){
temp = "女";
}
}catch(Exception e){
System.out.println("tip>parseUserInfo gender " + e);
}
userInfo.put("sex", temp);
}
list = node.getElementsByTagName("qq");
if (list.getLength() > 0){
String temp = null;
try{
temp = list.item(0).getFirstChild().getNodeValue();
}catch(Exception e){
System.out.println("tip>parseUserInfo qq " + e);
}finally{
if (null == temp) temp = "";
}
userInfo.put("qq", temp);
}
list = node.getElementsByTagName("msn");
if (list.getLength() > 0){
String temp = null;
try{
temp = list.item(0).getFirstChild().getNodeValue();
}catch(Exception e){
System.out.println("tip>parseUserInfo msn " + e);
}finally{
if (null == temp) temp = "";
}
userInfo.put("msn", temp);
}
list = node.getElementsByTagName("phone");
if (list.getLength() > 0){
String temp = null;
try{
temp = list.item(0).getFirstChild().getNodeValue();
}catch(Exception e){
System.out.println("tip>parseUserInfo phone " + e);
}finally{
if (null == temp) temp = "";
}
userInfo.put("phone", temp);
}
list = node.getElementsByTagName("credit");
if (list.getLength() > 0){
int temp = 0;
try{
temp = Integer.parseInt(list.item(0).getFirstChild().getNodeValue());
}catch(Exception e){
System.out.println("tip>parseUserInfo credit " + e);
}
userInfo.put("credit", new Integer(temp) );
}
list = node.getElementsByTagName("memberTitle");
if (list.getLength() > 0){
String temp = null;
try{
temp = list.item(0).getFirstChild().getNodeValue();
}catch(Exception e){
System.out.println("tip>parseUserInfo title " + e);
}finally{
if (null == temp) temp = "";
}
userInfo.put("title", temp );
}
list = node.getElementsByTagName("titleImage");
if (list.getLength() > 0){
String temp = null;
try{
temp = list.item(0).getFirstChild().getNodeValue();
}catch(Exception e){
System.out.println("tip>parseUserInfo img " + e);
}finally{
if (null == temp) temp = "";
}
userInfo.put("img", temp);
}
list = node.getElementsByTagName("level");
if (list.getLength() > 0){
int temp = 0;
try{
temp = Integer.parseInt(list.item(0).getFirstChild().getNodeValue());
}catch(Exception e){
System.out.println("tip>parseUserInfo level " + e);
}
userInfo.put("level", new Integer(temp) );
}
list = node.getElementsByTagName("address");
if (list.getLength() > 0){
String temp = null;
try{
temp = list.item(0).getFirstChild().getNodeValue();
}catch(Exception e){
System.out.println("tip>parseUserInfo addr " + e);
}finally{
if (null == temp) temp = "";
}
userInfo.put("addr", temp);
}
}catch(Exception e){
System.out.println("tip>parseUserInfo map.put " + e);
throw new Exception("提取用户资料出错");
}
this.reader.setUserInfo(userInfo);
}
---------
没心机为了实现这个而自己写一个统一的处理,在群中一问才知道.有人已经写好.系列成类中的成员的库
如xStream.
会自动的把xml系列成类中的成员变量;
这样就极其简单了
但是会有性能上的损失,因为通用性就损失性能
----
后来测试了一下,发现Xstream不适合bb使用.引用的不存在类太多了,修改没必要.
找到一个kxml2-min-2.3.0.jar,经过处理后就能使用.处理方式在后面文章中有
这个只是方便一点处理而已.但是却不能xml 2 class;
但是看了一下示例.发现还是要if(tagname) tagNameVar = tagValue形式;
还是没有一个统一的接口不需要判断,直接把xml转成变量
------
再找到sax2.
http://sourceforge.net/projects/sax/
回头看bb的api,发现已经有这东西......
看完了说明,发现它只是dom的替代品.只是内存使用上会更优.基于事件触发.
想了一下.发现还不适合多级的xml结构.要是想把多级的装到对象中时,会很麻烦;
测试代码:
package com.iximo.object;
import java.io.InputStream;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;
public class XmlSax extends DefaultHandler
{
public XmlSax ()
{
super();
}
public void xmlParse (InputStream r)
throws Exception
{
XMLReader xr = XMLReaderFactory.createXMLReader();
XmlSax handler = new XmlSax();
xr.setContentHandler(handler);//设置内容事件
xr.setErrorHandler(handler);//设置出错事件,需要实现各个方法,否则使用默认的方法.有异常,警告等不同级别
xr.parse(new InputSource(r));//解析流,可以是任何inputStream
}
public void startDocument (){
System.out.println("tip>Start sax document");
}
public void endDocument () {
System.out.println("tip>End sax document");
}
public void startElement (String uri, String name,
String qName, Attributes atts){
if ("".equals (uri))
System.out.println("Start element: " + qName);
else
System.out.println("Start element: {" + uri + "}" + name);
}
public void endElement (String uri, String name, String qName)
{
if ("".equals (uri))
System.out.println("End element: " + qName);
else
System.out.println("End element: {" + uri + "}" + name);
}
public void characters (char ch[], int start, int length)
{
System.out.print("Characters: \"");
for (int i = start; i < start + length; i++) {
switch (ch[i]) {
case '\\':
System.out.print("\\\\");
break;
case '"':
System.out.print("\\\"");
break;
case '\n':
System.out.print("\\n");
break;
case '\r':
System.out.print("\\r");
break;
case '\t':
System.out.print("\\t");
break;
default:
System.out.print(ch[i]);
break;
}
}
System.out.print("\"\n");
}
}
-----
xml
<?xml version="1.0" encoding="UTF-8" ?><error><error_id>501</error_id><error_info>unthorize</error_info></error>
----输出
[0.0] tip>Start sax document
[0.0] Start element: error
[0.0] Start element: error_id
[0.0] Characters: "501"
[0.0] End element: error_id
[0.0] Start element: error_info
[0.0] Characters: "unthorize"
[0.0] End element: error_info
[0.0] End element: error
[0.0] tip>End sax document
-----
麻烦.没找到合适的....
标签:Exception,temp,dom,--,list,System,println,写法,out From: https://blog.51cto.com/u_252283/6180647