52,796
社区成员
发帖
与我相关
我的任务
分享
/**
* 解析二维简单格式的信息类型内容格式定义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;
}
/**
* 简单消息 根据定义格式与内容组装成infoDetail对应的XML字符串
*
* @param allFields:定义格式
* @param infoDetails:内容字段
* @return
*/
private String getSimpleInfoDetailByDefineAndCpInfo(String infoId, String typeId, String typeName, List<SimpleFieldPojo> allFields,
String[] infoDetails) {
Document infoDetail = null;
imageFileMap = null;
try {
infoDetail = DocumentHelper.createDocument();
infoDetail.setXMLEncoding(SystemConstants.XML_ENCODING);
// 创建根结点
Element root = infoDetail.addElement(SystemConstants.SIMPLEXML_FIELDNAME_ROOT);
// 为根结点添加信息编号id节点
Element id = root.addElement(SystemConstants.XML_FIELDNAME_FIELD_ID);
id.setText(infoId);
// 为根结点添加信息类型编号typeid节点
Element typeid = root.addElement(SystemConstants.XML_FIELDNAME_TYPEID);
typeid.setText(typeId);
// 为根结点添加信息类型名称typename节点
Element typename = root.addElement(SystemConstants.XML_FIELDNAME_TYPENAME);
typename.setText(typeName);
// 循环遍历添加每个field字段
for (int index = 0; index < allFields.size(); index++) {
Element field = root.addElement(SystemConstants.XML_FIELDNAME_FIELD);
String fieldText = infoDetails[index + 1];// 字段内容
SimpleFieldPojo eachField = allFields.get(index);
String fieldType = eachField.getType();// 字段类型
// 判断字段类型是否是图片,如果是图片类型,要将原来的图片名称转换成新图片名称用于排除重名问题
if (SystemConstants.IMAGE_TYPE_FLAG.equalsIgnoreCase(fieldType)) {
if (imageFileMap == null) {
imageFileMap = new TreeMap<String, String>();
}
String newImageFileName = null;
// 将新旧文件名以键值对保存到集合中
if (fieldText != null && !fieldText.trim().equals("")) {
fieldText = fieldText.trim();
// 如果原来不存在该图片名称,才添加进去,如果是重名的图片则不再添加,只需上传一个就行了。
if (!imageFileMap.containsKey(fieldText)) {
newImageFileName = Utils.transformImageFileName(fieldText);
imageFileMap.put(fieldText, newImageFileName);
}else{
newImageFileName = imageFileMap.get(fieldText);
}
if (newImageFileName != null)
fieldText = newImageFileName;
}
}
field.setText(fieldText);
field.addAttribute(SystemConstants.XML_FIELDNAME_FIELD_ID, eachField.getId());
field.addAttribute(SystemConstants.XML_FIELDNAME_FIELD_NAME, eachField.getName());
field.addAttribute(SystemConstants.XML_FIELDNAME_FIELD_TYPE, fieldType);
}
} catch (RuntimeException e) {
e.printStackTrace();
return SystemConstants.PARSEXML_EXCEPTION_CODE;
}
return infoDetail.asXML();
}
Document doc=DocmentHelper.parse(xml);//这样不可以吗?
Document xmlDoc = (Document )new XPPReader().read(new ByteArrayInputStream(xml.getBytes()));
read返回类型不匹配, 你强制转化下