Java tutorial
/* * Copyright 2012-2014 sammyun.com.cn. All rights reserved. * Support: http://www.sammyun.com.cn * License: http://www.sammyun.com.cn/license */ package com.sammyun.util; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.sammyun.interceptor.ExecuteTimeInterceptor; import com.sammyun.meta.BaseDto; import com.sammyun.meta.Dto; /** * XML??<br> * * @author xutianlong * @version 3.0 * @since 2014-06-07 */ public class XmlHelper { /** logger */ private static final Logger logger = LoggerFactory.getLogger(ExecuteTimeInterceptor.class); /** * ?XMLDto(?XML?) * * @param pStrXml ?XML * @return outDto Dto */ public static final Dto parseXml2DtoBasedNode(String pStrXml) { Dto outDto = new BaseDto(); String strTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; Document document = null; try { if (pStrXml.indexOf("<?xml") < 0) pStrXml = strTitle + pStrXml; document = DocumentHelper.parseText(pStrXml); } catch (DocumentException e) { logger.error( "==??:==\nXML??XML DOM?!" + "\n?:"); e.printStackTrace(); } if (document != null) { // ? Element elNode = document.getRootElement(); // ??Dto for (Iterator it = elNode.elementIterator(); it.hasNext();) { Element leaf = (Element) it.next(); outDto.put(leaf.getName().toLowerCase(), leaf.getData()); } } return outDto; } /** * ?XMLDto(?XML?) * * @param pStrXml ?XML * @param pXPath ("//paralist/row" paralistrowxPath) * @return outDto Dto */ public static final Dto parseXml2DtoBasedNode(String pStrXml, String pXPath) { Dto outDto = new BaseDto(); String strTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; Document document = null; try { if (pStrXml.indexOf("<?xml") < 0) pStrXml = strTitle + pStrXml; document = DocumentHelper.parseText(pStrXml); } catch (DocumentException e) { logger.error( "==??:==\nXML??XML DOM?!" + "\n?:"); e.printStackTrace(); } if (document != null) { // ? Element elNode = document.getRootElement(); // ??Dto for (Iterator it = elNode.elementIterator(); it.hasNext();) { Element leaf = (Element) it.next(); outDto.put(leaf.getName().toLowerCase(), leaf.getData()); } } return outDto; } /** * ?XMLDto(?XML?) * * @param pStrXml ?XML * @param pXPath ("//paralist/row" paralistrowxPath) * @return outDto Dto */ public static final Dto parseXml2DtoBasedProperty(String pStrXml, String pXPath) { Dto outDto = new BaseDto(); String strTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; Document document = null; try { if (pStrXml.indexOf("<?xml") < 0) pStrXml = strTitle + pStrXml; document = DocumentHelper.parseText(pStrXml); } catch (DocumentException e) { logger.error( "==??:==\nXML??XML DOM?!" + "\n?:"); e.printStackTrace(); } if (document != null) { // ?Xpath? Element elRoot = (Element) document.selectSingleNode(pXPath); // ??Dto for (Iterator it = elRoot.attributeIterator(); it.hasNext();) { Attribute attribute = (Attribute) it.next(); outDto.put(attribute.getName().toLowerCase(), attribute.getData()); } } return outDto; } /** * Dto??XML?(?) * * @param dto Dto * @param pRootNodeName ?? * @return string XML? */ public static final String parseDto2Xml(Dto pDto, String pRootNodeName) { if (EduUtil.isEmpty(pDto)) { logger.warn("DTO,"); return "<root />"; } Document document = DocumentHelper.createDocument(); // document.addElement(pRootNodeName); Element root = document.getRootElement(); Iterator keyIterator = pDto.keySet().iterator(); while (keyIterator.hasNext()) { String key = (String) keyIterator.next(); String value = pDto.getAsString(key); Element leaf = root.addElement(key); leaf.setText(value); } // XML? String outXml = document.asXML().substring(39); return outXml; } /** * Dto??XML?(?) * * @param pDto Dto * @param pRootNodeName ?? * @param pFirstNodeName ?? * @return string XML? */ public static final String parseDto2Xml(Dto pDto, String pRootNodeName, String pFirstNodeName) { if (EduUtil.isEmpty(pDto)) { logger.warn("DTO,"); return "<root />"; } Document document = DocumentHelper.createDocument(); // document.addElement(pRootNodeName); Element root = document.getRootElement(); root.addElement(pFirstNodeName); Element firstEl = (Element) document.selectSingleNode("/" + pRootNodeName + "/" + pFirstNodeName); Iterator keyIterator = pDto.keySet().iterator(); while (keyIterator.hasNext()) { String key = (String) keyIterator.next(); String value = pDto.getAsString(key); firstEl.addAttribute(key, value); } // XML? String outXml = document.asXML().substring(39); return outXml; } /** * List???XML?(?) * * @param pList List?(List?Dto?VO?Domain) * @param pRootNodeName ?? * @param pFirstNodeName ?? * @return string XML? */ public static final String parseList2Xml(List pList, String pRootNodeName, String pFirstNodeName) { Document document = DocumentHelper.createDocument(); Element elRoot = document.addElement(pRootNodeName); for (int i = 0; i < pList.size(); i++) { Dto dto = (Dto) pList.get(i); Element elRow = elRoot.addElement(pFirstNodeName); Iterator it = dto.entrySet().iterator(); while (it.hasNext()) { Dto.Entry entry = (Dto.Entry) it.next(); elRow.addAttribute((String) entry.getKey(), String.valueOf(entry.getValue())); } } String outXml = document.asXML().substring(39); return outXml; } /** * List???XML?(?) * * @param pList List?(List?Dto?VO?Domain) * @param pRootNodeName ?? * @param pFirstNodeName ?? * @return string XML? */ public static final String parseList2XmlBasedNode(List pList, String pRootNodeName, String pFirstNodeName) { Document document = DocumentHelper.createDocument(); Element output = document.addElement(pRootNodeName); for (int i = 0; i < pList.size(); i++) { Dto dto = (Dto) pList.get(i); Element elRow = output.addElement(pFirstNodeName); Iterator it = dto.entrySet().iterator(); while (it.hasNext()) { Dto.Entry entry = (Dto.Entry) it.next(); Element leaf = elRow.addElement((String) entry.getKey()); leaf.setText(String.valueOf(entry.getValue())); } } String outXml = document.asXML().substring(39); return outXml; } /** * XMLList(XML?) * * @param pStrXml ?XML? * @return list List */ public static final List parseXml2List(String pStrXml) { List lst = new ArrayList(); String strTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; Document document = null; try { if (pStrXml.indexOf("<?xml") < 0) pStrXml = strTitle + pStrXml; document = DocumentHelper.parseText(pStrXml); } catch (DocumentException e) { logger.error( "==??:==\nXML??XML DOM?!" + "\n?:"); e.printStackTrace(); } if (document != null) { // ? Element elRoot = document.getRootElement(); // ?? Iterator elIt = elRoot.elementIterator(); while (elIt.hasNext()) { Element el = (Element) elIt.next(); Iterator attrIt = el.attributeIterator(); Dto dto = new BaseDto(); while (attrIt.hasNext()) { Attribute attribute = (Attribute) attrIt.next(); dto.put(attribute.getName().toLowerCase(), attribute.getData()); } lst.add(dto); } } return lst; } }