日期:2014-05-16 浏览次数:20637 次
Document xmlDoc = (Document )new XPPReader().read(new ByteArrayInputStream(xml.getBytes()));
------解决方案--------------------
Document doc=DocmentHelper.parse(xml);//这样不可以吗?
------解决方案--------------------
贴两段dom4j操作xml代码供参考啊,别笑话啊,只希望对LZ有帮助!个人感觉dom4j的API操作还是很方便与容易的。
1、dom4j解析XML
/** * 解析二维简单格式的信息类型内容格式定义XML * * @param xmlSchema * @return */ private SimpleMultiFieldDefine parseSimpleXmlSchemaByXmlString(String xmlSchema) { Document doc = null; SimpleMultiFieldDefine simpleDefine = null; List<SimpleFieldPojo> allFields = null; try { // 将字符串转换成文档对象 doc = DocumentHelper.parseText(xmlSchema); // 获取根节点 Element root = doc.getRootElement(); simpleDefine = new SimpleMultiFieldDefine(); // 获取消息类型节点 String typeId = root.element(SystemConstants.XML_FIELDNAME_TYPEID).getTextTrim(); simpleDefine.setTypeId(typeId); // 获取消息名称节点 String typeName = root.element(SystemConstants.XML_FIELDNAME_TYPENAME).getTextTrim(); simpleDefine.setTypeName(typeName); // 循环遍历获取所有field字段 allFields = new ArrayList<SimpleFieldPojo>(); for (Iterator fields = root.elementIterator(SystemConstants.XML_FIELDNAME_FIELD); fields.hasNext();) { Element element = (Element) fields.next(); String field_id = element.elementTextTrim(SystemConstants.XML_FIELDNAME_FIELD_ID); String field_name = element.elementTextTrim(SystemConstants.XML_FIELDNAME_FIELD_NAME); String field_type = element.elementTextTrim(SystemConstants.XML_FIELDNAME_FIELD_TYPE); SimpleFieldPojo field = new SimpleFieldPojo(field_id, field_name, field_type); allFields.add(field); } simpleDefine.setAllFields(allFields); } catch (DocumentException e) { return null; } return simpleDefine; }