`
huqingyong_eye
  • 浏览: 36528 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

XML解析

阅读更多
package com.nci.nportal.webservice.allinterface.cisinterface;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.jdom.input.SAXBuilder;

import com.nci.nportal.webservice.Encode;
import com.nci.nportal.webservice.allinterface.OutInformationService;

/**
 * 类说明:
 */
public class XmlUtils {
	/**
	 * <?xml version="1.0" encoding="UTF-8"?>
	 *<invoke type="invoke.createPlan" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="invoke.xsd" sendoraccept="accept" whichservice="outinformation'>
	 *<return type="return" />
	 *<parameters>
	 *	<object name="data'>
	 *	<property name="title">承德市烟草专卖局关于XX同志上班期间玩游戏的处罚决定</property>"
	 *	</object>
	 *</parameters>
	 *</invoke>
	 */
	private Node selectSingleNode(Document document, String path){
		List list = document.selectNodes(path);
		if(list!=null && list.size()>0){
			for(int i=0;i<list.size();i++){
				Object obj = list.get(i);
				if(obj!=null && obj instanceof Node){
					return (Node)obj;
				}
			}
		}
		return null;
		
	}
	
	/**
	 * <?xml version="1.0" encoding="UTF-8"?>
	 *<invoke type="invoke.createPlan" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="invoke.xsd" sendoraccept="accept" whichservice="outinformation'>
	 *<return type="return" />
	 *<parameters>
	 *	<object name="data'>
		 *	<property name="formlist'>
		 *		<list>
		 *			<property name=" projectids '>1,2,3</property>
		 *			<property name=" projecttypes '>a,b,c</property>
		 *			<property name=" contents '><![CDATA[这是内容]]></property>
		 *		</list>
		 *	</property>
	 *	</object>
	 *</parameters>
	 *</invoke>
	 */
	private ArrayList getListAttributeValue(Document document,String listType,String attribute){
		ArrayList attributevalue = new ArrayList();
		Element root = document.getRootElement();
		Element parameters = (Element)root.elements().get(1); //<parameters>
		Element object = (Element)parameters.elements().get(0); 
		List property = object.elements(); 
		for (int i = 0; i<property.size();i++) {
			Element e = (Element) property.get(i);
			Attribute attr = (Attribute) e.attributeIterator().next();
			if(attr.getText().equals(listType)){
				Element fl = (Element)e.elements().get(e.elements().size()-1);
				List formlist = fl.elements();
				for(int j =0;j<formlist.size();j++){
					Element ee= (Element)formlist.get(j);
					Attribute attr2 = (Attribute)ee.attributeIterator().next();
					if(attr2.getText().equals(attribute)){
						ee.asXML();
						attributevalue.add(ee.getText());
					}
				}
				break;
			}
		}
		return attributevalue;
	}
	
	/**
	 * <property name="attachlist'>
*			<list>
*				<object name="attachment'>
*					<!--附件名称-->
*					<property name="attachname'>劳动法.doc</property>
*					<!--附件地址-->
*					<property name="attachurl'>http://172.0.0.1/nportal/files/test.doc</property>
	*				<!--附件内容-->
	*				<property name="attachcontent'> MjAwOMTqwM22r7eouea2qMjnz8Kjug0KyKvE6rzZxtoxMczso7oNCsflw/ehotbQx+8= 8Kjug0KyKvE6rzZxtoxMczso7oNCsflw/ehotb </property>
	*			</object>
	*			<object name=" attachment'>
	*				<!--附件名称-->
	*				<property name="attachname'>附件.jar</property>
	*				<!--附件地址-->
	*				<property name="attachurl'>http://172.0.0.1/nportal/files/test.doc</property>
	*				<!--附件内容-->
	*				<property name="attachcontent'> MjAwOMTqwM22r7eouea2qMjnz8Kjug0KyKvE6rzZxtoxMczso7oNCsflw/ehotbQx+8= 8Kjug0KyKvE6rzZxtoxMczso7oNCsflw/ehotb </property>
	*			</object>
	*		</list>
	*	</property>
	 */
	private ArrayList getMoreListAttributeValue(Document document,String listType,String objname,String attribute){
		ArrayList attributevalue = new ArrayList();
		Element root = document.getRootElement();
		Element parameters = (Element)root.elements().get(1); //<parameters>
		Element object = (Element)parameters.elements().get(0); 
		List property = object.elements(); 
		for (int i = 0; i<property.size();i++) {
			Element e = (Element) property.get(i);
			Attribute attr = (Attribute) e.attributeIterator().next();
			//<property name='picturelist'>
			if(attr.getText().equals(listType)){
				//<list>
				Element list = (Element) e.elements().get(0);
				//<object name="picture'>
				List obj = list.elements();
				for (int j =0;j<obj.size(); j++) {
					Element obj2 = (Element) obj.get(j);
					Attribute attr2 = (Attribute) obj2.attributeIterator().next();
					if(attr2.getText().equals(objname)){
						List obj3 = obj2.elements();
						for (int k =0;k<obj3.size(); k++) {
							Element property_sec= (Element)obj3.get(k);
							Attribute provalue = (Attribute)property_sec.attributeIterator().next();
							if(provalue.getText().equals(attribute)){
								attributevalue.add(property_sec.getText());
							}
						}
					}
				}
				break;
			}
		}
		return attributevalue;
	}
	
	public static void main(String[] args){
		OutInformationService a = new OutInformationService();
		SAXBuilder   bld=new   SAXBuilder(); 
		String invokeDoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
	+ " <invoke type=\"invoke.createPlan\""
	+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
	+ " xsi:noNamespaceSchemaLocation=\"invoke.xsd\" sendoraccept=\"accept\""
	+ " whichservice=\"outinformation\">"
	+ "        <return type=\"return\"/>"
	+ "        <parameters>"
	+ "                <object name=\"data\">"
	+ "                        <property name=\"title\">承德市烟草专卖局关于XX同志上"
	+ "班期间玩游戏的处罚决定</property>"
	+ "                        <property name=\"formlist\">"
	+ "                        <list>"
	+ "                                <property name=\"projectids\"></property>"
	+ "                                <property name=\"contents\"><![CDATA[PGEgaHJlZj0naHR0cDovLzE5Mi4xNjguMTMzLjE2Njo4MDg4L2l"
	+ "vYS9kby9pb2EvaW9hY2Rvd25sb2FkP2NpZD1jaGVuZ2Rlb2EmcmVxdWVzdHR5cGU9cHViZmlsZSZ"
	+ "maWxldHlwZT1zZW5kZmlsZSZndWlkPUVCMjM2MDFELUE3MTItQzJEMi0yNTU5LUEwQ0FEMEI2OTA"
	+ "4Qyc+o6jV/c7Eo6mz0LXCytDRzLLd16jC9L7WudjT2lhYzazWvsnPsODG2rzkzebTzs+3tcS"
	+ "4Qyc+0pre"
	+ "jvva2qDwvYT48YnI+]]></property>"
	+ "                        </list>"
	+ "                        </property>"
	+ "                        <property"
	+ " name=\"columnid\">0A5A9FF45262C2ACFA1E3FED2A66DFF6</property>"
	+ ""
	+ "                        <property name=\"publishdept\">cdyc</property>"
	+ "                        <property name=\"publisher\">超级用户</property>"
	+ "                        <property name=\"publisherid\">admin</property>"
	+ "                        <property name=\"disorder\">100</property>"
	+ "                        <property name=\"users\"></property>"
	+ "                        <property name=\"roles\"></property>"
	+ "                        <property"
	+ " name=\"cmscode\">EB23601D-A712-C2D2-2559-A0CAD0B6908C</property>"
	+ "                </object>"
	+ "        </parameters>"
	+ "</invoke>";
		SAXReader reader = new SAXReader();
		String xmlstr = Encode.stripNonValidXMLCharacters1(Encode.stripNonValidXMLCharacters(Encode.filterXmlData(invokeDoc)));
		Document document;
		try {
			document = reader.read(new ByteArrayInputStream(xmlstr.getBytes("UTF-8")));
			a.acceptInfo(document);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
	}
	public String testacceptInfo(String invokeDoc) {
		String returnValue = "";
		try {
			SAXReader reader = new SAXReader();
			String xmlstr = Encode
					.stripNonValidXMLCharacters1(Encode
							.stripNonValidXMLCharacters(Encode
									.filterXmlData(invokeDoc)));
			Document document = reader.read(new ByteArrayInputStream(xmlstr
					.getBytes("UTF-8")));
			document.toString();
			Element root = document.getRootElement();
			String title = selectSingleNode(document, "/invoke/parameters/object/property[normalize-space(@name)='title']").getText();//document.selectSingleNode("/invoke/parameters/object/property[normalize-space(@name)='meetingName']").getText();
			ArrayList projectids = getListAttributeValue(document,"formlist","projectids");
			ArrayList pictureurl = getMoreListAttributeValue(document,"picturelist","picture","pictureurl");
			String sendoraccept = root.attribute("sendoraccept").getText();

//			returnValue = doaccept(sendoraccept, document);
		} catch (UnsupportedEncodingException docEx) {
			docEx.printStackTrace();
			returnValue = "";
		} catch (DocumentException docEx) {
			docEx.printStackTrace();
			returnValue = "";
		}
		return returnValue;
	}

}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics