dom4j-----解析xml工具类

/** * 解析XML,工具类 * @author LiBer.Li * 2015.3.7 * */public class Dom4jXml {/** * 根据xml字符串解析为YimallCategoryAttributeInfo对象 * * @param xml * 接口返回的xml对应YimallCategoryAttributeInfo格式数据信息 * @return */public static YimallCategoryAttributeInfo xmlToYimallCategoryAttributeInfo(String xml, int num) {// 定义YimallCategoryAttributeInfo对象,以便返回该类型数据YimallCategoryAttributeInfo yimallCategoryAttributeInfo = new YimallCategoryAttributeInfo();try {// 根据返回的xml字符串转化为Dom4j文档Document document = DocumentHelper.parseText(xml);// 获取根节点Element rootElement = document.getRootElement();// 获取根节点下名字为categoryAttributeInfoList节点,下面同理Element categoryAttributeInfoListElement = rootElement.element("categoryAttributeInfoList");List categoryAttributeInfoElements = categoryAttributeInfoListElement.elements("categoryAttributeInfo");Element categoryAttributeInfoElement = categoryAttributeInfoElements.get(num);// 获取该类型数据的属性并相应赋值// attributeidyimallCategoryAttributeInfo.setAttributeId(getElementByAttr(categoryAttributeInfoElement, "attributeId"));// attributeNameyimallCategoryAttributeInfo.setAttributeName(getElementByAttr(categoryAttributeInfoElement, "attributeName"));// valueTypeyimallCategoryAttributeInfo.setValueType(getElementByAttr(categoryAttributeInfoElement, "valueType"));// editTypeyimallCategoryAttributeInfo.setEditType(getElementByAttr(categoryAttributeInfoElement, "editType"));//type 、 unit 这两个属性在返回字符串里不存在了/*// typeyimallCategoryAttributeInfo.setType(getElementByAttr(categoryAttributeInfoElement, "type"));// unityimallCategoryAttributeInfo.setUnit(getElementByAttr(categoryAttributeInfoElement, "unit"));*/// isMustEnteryimallCategoryAttributeInfo.setIsMustEnter(getElementByAttr(categoryAttributeInfoElement, "isMustEnter"));// isDescribeAttributeyimallCategoryAttributeInfo.setIsDescribeAttribute(getElementByAttr(categoryAttributeInfoElement, "isDescribeAttribute"));// isGuideAttributeyimallCategoryAttributeInfo.setIsGuideAttribute(getElementByAttr(categoryAttributeInfoElement, "isGuideAttribute"));// isSeriesAttributeyimallCategoryAttributeInfo.setIsSeriesAttribute(getElementByAttr(categoryAttributeInfoElement, "isSeriesAttribute"));// attributeItemInfoListyimallCategoryAttributeInfo.setAttributeItemInfoList(getElementByAttr(categoryAttributeInfoElement,"attributeItemInfoList"));System.out.println("正在下载属性信息……");// categoryid这个看下怎么处理// yimallshopid这个看下怎么处理// delFlag这个看下怎么处理} catch (DocumentException e) {// TODO Auto-generated catch blockSystem.out.println("解析XML数据异常……");e.printStackTrace();}return yimallCategoryAttributeInfo;}//判断无素是否为空函数public static boolean isNull(Element element) {if (element != null && "".equals(element)) {return true;} else {return false;}}/** * 得到element结点的attr子结点的值,并以返回字符串 * * @param element * 源结点 * @param attr * 子结点名称 * @return */public static String getElementByAttr(Element element, String attr) {Element tempElement = element.element(attr);if (!isNull(tempElement)) {// System.out.println("xml single value: " + tempElement.getText());return tempElement.getTextTrim();}return "";}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值