dom4j 解析 xml标签属性


重写onEnd()和onStart()方法

public class XmlElementHandler implements ElementHandler {

@Override
public void onEnd(ElementPath arg0) {
Element ele = arg0.getCurrent();
String name = ele.getName();
try 
{
//保存"MT"标签的属性到内存
if("MT".equals(name))
{
String mTvalue = ele.getTextTrim();
ParaCollect.getInstance().put("MT", mTvalue);
}
//保存"SubNo"标签的属性到内存
if("SubNo".equals(name))
{
String subNovalue = ele.getTextTrim();
ParaCollect.getInstance().put("localPeriods", subNovalue);
}
//保存"Freq"标签的属性到内存
if("Freq".equals(name))
{
String Freqvalue = ele.getTextTrim();
ParaCollect.getInstance().put("localFreq", Freqvalue);
}
//保存"SubCode"标签的属性到内存
if("SubCode".equals(name))
{
String SubCodevalue = ele.getTextTrim();
ParaCollect.getInstance().put("localSubCode", SubCodevalue);
}
} catch (Exception e) {
}
}
@Override
public void onStart(ElementPath arg0) { }
}

获取标签属性

String xmlps = “xml文件路径”;

/*************解析xml中标签SubNo的属性 begin************************/
SAXReader reader;
try
{
InputStream in= new FileInputStream(new File(xmlps));
ElementHandler manElementHandler=(ElementHandler) new XmlElementHandler();
reader=new SAXReader();
reader.addHandler("/Document/SubNo", manElementHandler);
reader.addHandler("/Document/Freq", manElementHandler);
reader.addHandler("/Document/SubCode", manElementHandler);
reader.read(in);
}
catch (Exception e) 
{
log.error("公共检查解析xml异常", e);
}

posted @ 2014-12-21 00:33  JAVA之迷  阅读(146)  评论(0编辑  收藏  举报