xml解析dom4j方式
public class XmlParse {
public static int ELEMENT_TYPE_HEADER = 1;
public static int ELEMENT_TYPE_BODY = 2;
private Document document;
private Element headerElement;
private Element rootElement;
private Element bodyElement;
private Element mdElement;
public void addHeaderElement(String nodeName, String content) {
if ((content != null) && (content.length() > 0))
this.headerElement.addElement(nodeName).setText(content);
}
public XmlParse(){
}
/**
* 拼串
*
* @param rootElementName
* @param headerElementName
* @param bodyElemnetName
*/
public XmlParse(String rootElementName, String headerElementName,
String bodyElemnetName) {
this.document = DocumentHelper.createDocument();
this.rootElement = this.document.addElement(rootElementName);
if (StringUtils.isNotEmpty(headerElementName))
this.headerElement = this.rootElement.addElement(headerElementName);
if (StringUtils.isNotEmpty(bodyElemnetName))
this.bodyElement = this.rootElement.addElement(bodyElemnetName);
}
public XmlParse(String rootElementName, String headerElementName) {
this.document = DocumentHelper.createDocument();
this.rootElement = this.document.addElement(rootElementName);
this.headerElement = this.rootElement.addElement(headerElementName);
}
public XmlParse(String rootElementName) {
this.document = DocumentHelper.createDocument();
System.out.println(this.document);
this.bodyElement = this.document.addElement(rootElementName);
System.out.println(this.bodyElement);
}
/**
* 解析返回内容
*
* @param returnContent
* @throws DocumentException
*/
public XmlParse(String returnContent, String head, String body, String type)
throws DocumentException {
this.document = DocumentHelper.parseText(returnContent);
this.rootElement = this.document.getRootElement();
this.headerElement = this.rootElement.element(head);
this.bodyElement = this.rootElement.element(body);
}
public String getElementAttribute(String nodeName, String attributeName,int elementType) {
Element nodeElement = getPathElement(nodeName, elementType);
return nodeElement.attributeValue(attributeName);
}
/**
*
* @param nodeName
* @param elementType 1head 2body
* @return
*/
public String getElementAttributeText(String nodeName, int elementType) {
Element nodeElement = getPathElement(nodeName, elementType);
return nodeElement.getText();
}
public String getBodyText(String nodeName) {
return bodyElement.getText();
}
/**
* 获取属性值
* @param nodeElement
* @return
*/
public static HashMap<String, String> getElementAttribute(Element nodeElement)
{
if (nodeElement != null) {
HashMap<String,String> returnMap = new HashMap<String,String>();
for (Iterator<?> it = nodeElement.attributeIterator(); it.hasNext(); ) {
Attribute nodeAttribute = (Attribute)it.next();
returnMap.put(nodeAttribute.getName(), nodeAttribute.getText());
}
return returnMap;
}
return null;
}
private Element getPathElement(String nodeName,int elementType) {
Element element = null;
if (elementType == ELEMENT_TYPE_HEADER) {
element = this.headerElement.element(nodeName);
} else if (elementType == ELEMENT_TYPE_BODY){
element = this.bodyElement.element(nodeName);
}
return element;
}
/**
* 解析element 得到根节点
* @return
* @throws DocumentException
*/
public static Element getRootElement(String strs) throws DocumentException{
Document doc = DocumentHelper.parseText(strs);
Element rootElt = doc.getRootElement();
return rootElt;
}
/**
* 解析xml下迭代 list属性 name、sex
* @param strs 要解析的xml串
* @param nodeName 取得节点父节点 response/alipay_results/
* @param rootElement 获取循环根节点 people
*/
public static List<HashMap<String,String>> getElementTextList(String nodeName,String rootElement,String strs) throws DocumentException{
Element rootElt=getRootElement(strs);
List<HashMap<String,String>>lists=new ArrayList<HashMap<String,String>>();
Iterator<?> iterator=null;
String[] nodePathList = StringUtils.split(nodeName, "/");
for(int i=0;i<nodePathList.length;i++){
rootElt=rootElt.element(nodePathList[i]+"");
}
iterator=rootElt.elementIterator(rootElement);
while(iterator.hasNext()){
HashMap<String,String> returnMap = new HashMap<String,String>();
Element ele=(Element)iterator.next();
List<?> nodes = ele.elements();
for (Iterator<?> it1 = nodes.iterator(); it1.hasNext(); ) {
Element node = (Element)it1.next();
returnMap.put(node.getName(), node.getText());
}
lists.add(returnMap);
}
return lists;
}
/**
* 解析xml下迭代 属性 name、sex
* @param strs 要解析的xml串
* @param nodeName 取得节点父节点 /response
* @param subElement 获取循环根节点 alipay_results
* @return
* @throws DocumentException
*/
public static HashMap<String,String> getElementText(String nodeName,String rootElement,String strs) throws DocumentException{
Element ele=getElement(nodeName,rootElement,strs);
HashMap<String,String> returnMap = new HashMap<String,String>();
List<?> nodes = ele.elements();
for (Iterator<?> it1 = nodes.iterator(); it1.hasNext(); ) {
Element node = (Element)it1.next();
returnMap.put(node.getName(), node.getText());
}
return returnMap;
}
/**
* 解析xml下迭代 属性 name、sex
* @param strs 要解析的xml串
* @param nodeName 取得节点父节点 /response
* @param subElement 获取循环根节点 alipay_results
* @return
* @throws DocumentException
*/
public static HashMap<String,String> getElementAttribute(String nodeName,String rootElement,String strs) throws DocumentException{
Element ele=getElement(nodeName,rootElement,strs);
HashMap<String,String> returnMap = new HashMap<String,String>();
List<?> nodes = ele.attributes();
for (Iterator<?> it1 = nodes.iterator(); it1.hasNext(); ) {
Attribute node =(Attribute) it1.next();
returnMap.put(node.getName(),node.getValue());
}
return returnMap;
}
/**
* 读取属性list
* @param nodeName body/response/result/
* @param rootElement code
* @param strs
* @return"<ticket><body><response errorcode=\"错误码\"><result>
* @throws DocumentException
*/
public static List<HashMap<String,String>> getElementAttributeList(String nodeName,String rootElement,String strs) throws DocumentException{
Element rootElt=getRootElement(strs);
Iterator<?> iterator=null;
String[] nodePathList = StringUtils.split(nodeName, "/");
for(int i=0;i<nodePathList.length;i++){
rootElt=rootElt.element(nodePathList[i]+"");
}
List<HashMap<String, String>>list=new ArrayList<HashMap<String,String>>();
Element ele=null;
iterator=rootElt.elementIterator(rootElement);
while(iterator.hasNext()){
HashMap<String,String> returnMap = new HashMap<String,String>();
ele=(Element)iterator.next();
List<?> nodes = ele.elements();
for (Iterator<?> it1 = nodes.iterator(); it1.hasNext(); ) {
Element node =(Element) it1.next();
returnMap.put(node.getName(),node.getText());
}
list.add(returnMap);
}
return list;
}
public static Element getElement(String nodeName,String rootElement,String strs) throws DocumentException{
Element rootElt=getRootElement(strs);
Iterator<?> iterator=null;
String[] nodePathList = StringUtils.split(nodeName, "/");
for(int i=0;i<nodePathList.length;i++){
rootElt=rootElt.element(nodePathList[i]+"");
}
Element ele=null;
iterator=rootElt.elementIterator(rootElement);
if(iterator.hasNext()){
ele=(Element)iterator.next();
}
return ele;
}
public void addHeaderAttribute(Map<String, String> attributeMap) {
if ((attributeMap != null) && (!attributeMap.isEmpty())) {
Iterator<?> it = attributeMap.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
this.headerElement.addAttribute(key,
(String) attributeMap.get(key));
}
}
}
public void addBodyElement(Map<String, String> nodeMap) {
if ((nodeMap != null) && (!nodeMap.isEmpty())) {
Iterator<?> it = nodeMap.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
addBodyElement(key, (String) nodeMap.get(key));
}
}
}
public void addBodyElement(String nodeName, String content) {
if ((content != null) && (content.length() > 0))
this.bodyElement.setText(content);
}
public Element addBodyElementAndAttribute(String nodeName, String content,
Map<String, Object> attributeMap) {
Element bodyNodeElement = this.bodyElement.addElement(nodeName);
if ((content != null) && (content.length() > 0)) {
bodyNodeElement.setText(content);
}
return addElementAttribute(bodyNodeElement, attributeMap);
}
public Element addElementAttribute(Element element,
Map<String, Object> attributeMap) {
if ((attributeMap != null) && (!attributeMap.isEmpty())) {
Iterator<?> it = attributeMap.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
element.addAttribute(key, attributeMap.get(key).toString().toString());
}
}
return element;
}
public String asBodyXML() {
return this.bodyElement.asXML();
}
public String asXML() {
return this.document.asXML();
}
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
public Element getHeaderElement() {
return headerElement;
}
public void setHeaderElement(Element headerElement) {
this.headerElement = headerElement;
}
public Element getRootElement() {
return rootElement;
}
public void setRootElement(Element rootElement) {
this.rootElement = rootElement;
}
public Element getBodyElement() {
return bodyElement;
}
public void setBodyElement(Element bodyElement) {
this.bodyElement = bodyElement;
}
public Element getMdElement() {
return mdElement;
}
public void setMdElement(Element mdElement) {
this.mdElement = mdElement;
}
public static void main(String[] args) throws IOException {
}
public static HashMap<String,String> getElementAttribute(String strs) throws DocumentException{
Element ele=getRootElement(strs);
HashMap<String,String> returnMap = new HashMap<String,String>();
List<?> nodes = ele.elements();
for (Iterator<?> it1 = nodes.iterator(); it1.hasNext(); ) {
Element node =(Element) it1.next();
returnMap.put(node.getName(),node.getText());
}
return returnMap;
}
}