`

一些xml的java代码

 
阅读更多
/**
	 * 
	 * @param xml
	 * @return
	 * @Exception RDCommunicationException 1 critical item is null. 
	 * 				2 document is illegal. 
	 * 				3 the type of value is not anticipated.
	 * @param 
	 * @return List<String> this result's sequence is
	 * 0 : Feature 			optional
	 * 1 : Property Unit 	critical
	 * 2 : Property Name 	critical
	 * 3 : Model 			critical
	 * 4 : geo:long 		optional
	 * 5 : geo:lat 			optional
	 * 6 : geo:alt 			optional
	 * 7 : gn:name 			optional
	 */
	public Map<String,String> analyzerXml(String xml)throws RDCommunicationException
	{
		logger.info("XmlUtil.analyzerXml: Analyzing String :" + xml);
		
		Map<String,String> rel = new HashMap<String,String>();
		SAXReader reader = new SAXReader(false);
		String tag1 = "//"+namespace+":"+"information-type";
		String tag2 = "//"+namespace+":"+"location";
		String tag22 = "//"+"geo"+":"+"long";
		String tag23 = "//"+"geo"+":"+"lat";
		String tag24 = "//"+"geo"+":"+"alt";
		String tag25 = "//"+"gn"+":"+"name";
		reader.getDocumentFactory().setXPathNamespaceURIs(dealNamespace());
		Document doc = null;
		try {
			doc = reader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
		} catch (DocumentException e) {
			logger.error("XmlUtil.analyzerXml could not parse response from RD", e.fillInStackTrace());
			throw new RDCommunicationException("Could not parse response from RD",e);
		} catch (UnsupportedEncodingException e) {
			logger.error("XmlUtil.analyzerXml could not parse response from RD", e.fillInStackTrace());
			throw new RDCommunicationException("Could not parse response from RD",e);
		}
		Element root = doc.getRootElement();
		Element info;
		for (Iterator i = doc.selectNodes(tag1).iterator(); i.hasNext();){
			info = (Element)i.next();
			try{
			Element feature = (Element)info.elementIterator("Feature").next();
			rel.put("Feature",feature.getText());
			}catch(Exception e)
			{
				logger.error("XmlUtil.analyzerXml. Feature is null. ", e.fillInStackTrace());
			}
			try{
			Element property = (Element)info.elementIterator("Property").next();
			rel.put("Property Unit",((Element)property.elementIterator("Unit").next()).getText());
			rel.put("Property Name",((Element)property.elementIterator("Name").next()).getText());
			}catch(Exception e)
			{
				logger.error("XmlUtil.analyzerXml. Property is null.", e.fillInStackTrace());
				//throw new RDCommunicationException("Could not parse response from RD",e);
			}
		}
		
		
		Element resource = root.element("Resource-Description");
		try{
		for (Iterator i = resource.elementIterator("Model"); i.hasNext();) {
			Element model = (Element) i.next();
			rel.put("Model",model.getText());
		}
	}catch(Exception e)
	{
		logger.error("XmlUtil.analyzerXml. Model is null", e.fillInStackTrace());
		//throw new RDCommunicationException("Could not parse response from RD",e);
	}
		for (Iterator i = doc.selectNodes(tag2).iterator(); i.hasNext();) {
			info = (Element) i.next();
			try{
			rel.put("geo:long",((Element) info.selectNodes(tag22).iterator().next())
					.getText());
			}catch(Exception e)
			{
				logger.info("There is no element" + tag22 + "in result xml file");
			}
			try{
			rel.put("geo:lat",((Element) info.selectNodes(tag23).iterator().next())
					.getText());
			}catch(Exception e)
			{
				logger.info("There is no element" + tag22 + "in result xml file");
			}
			try{
			rel.put("geo:alt",((Element) info.selectNodes(tag24).iterator().next())
					.getText());
			}catch(Exception e)
			{
				logger.info("There is no element" + tag22 + "in result xml file");
			}
			try{
			rel.put("gn:name",((Element) info.selectNodes(tag25).iterator().next())
					.getText());
			}catch(Exception e)
			{
				logger.info("There is no element" + tag22 + "in result xml file");
			}
		}
		return rel;
	}
//	public List<String> analyzerXml(InputStream is)
//	{
//		List<String> rel = new ArrayList<String>();
//		//String result = null;
//		SAXReader reader = new SAXReader();
//		
//		String tag = "//"+namespace+":"+"information-type";
//		reader.getDocumentFactory().setXPathNamespaceURIs(dealNamespace());
//		Document doc = null;
//		try {
//			doc = reader.read(is);
//		} catch (DocumentException e) {
//			e.printStackTrace();
//			return rel;
//		}
//		
//		logger.info("XmlUtil.analyzerXml: Analyzing InputStream, doc.getText :" + doc.getText());
//		
//		Element root = doc.getRootElement();
//		Element info;
//		for (Iterator i = doc.selectNodes(tag).iterator(); i.hasNext();){
//			info = (Element)i.next();
//			Element feature = (Element)info.elementIterator("Feature").next();
//			rel.add(feature.getText());
//			Element property = (Element)info.elementIterator("Property").next();
//			rel.add(((Element)property.elementIterator("Unit").next()).getText());
//			rel.add(((Element)property.elementIterator("Name").next()).getText());
//		}
//		for (Iterator i = root.elementIterator("Model"); i.hasNext();) {
//			Element model = (Element) i.next();
//			rel.add(model.getText());
//		}
//		
//		return rel;
//	}
	private Map dealNamespace()
	{
		Map map = new HashMap();
	    map.put("ard","http://www.ict-sensei.org/AdvancedResourceDescription/");
	    map.put("gn","http://www.geonames.org/ontology#");
	    map.put("geo","http://www.w3.org/2003/01/geo/wgs84_pos#");
	    return map;

	}
	public List<String> getItemFromXml(InputStream is, String tag, 
										String namespace)
	{
		List<String> rel = new ArrayList<String>();
		//String result = null;
		SAXReader reader = new SAXReader();
		if(namespace != null && !namespace.equals(""))
		tag = "//"+namespace+":"+tag;
		reader.getDocumentFactory().setXPathNamespaceURIs(dealNamespace());
		Document doc = null;
		try {
			doc = reader.read(is);
		} catch (DocumentException e) {
			return rel;
		}
		Element root = doc.getRootElement();
		Element foo;
		for (Iterator i = doc.selectNodes(tag).iterator(); i.hasNext();){
			foo = (Element)i.next();
			rel.add(foo.getText());
		}
		return rel;
	}
	public List<String> getItemFromXml(String is, String tag) {
		List<String> rel = new ArrayList<String>();
		SAXReader reader = new SAXReader();
		
		Document doc = null;
		try {
			doc = reader.read(new ByteArrayInputStream(is.getBytes("UTF-8")));

		} catch (DocumentException e) {
			e.printStackTrace();
			return rel;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return rel;
		}
		Element root = doc.getRootElement();
		Element resource = root.element("ResourceDescription");
		Element foo;
		for (Iterator i = resource.elementIterator(tag); i.hasNext();) {
			foo = (Element) i.next();
			rel.add(foo.getText());
		}
		return rel;
	}

<Result><Matched>1</Matched><Resource-Description><Resource-ID>http://m2m.ericsson.com/sensors/causensor010</Resource-ID><Name>Fish pond sensor 1</Name><Model>cau:001</Model><Storage-ID>1</Storage-ID><Expiration-Time>12-04-21T15:02:30+00:00</Expiration-Time><Tag>Temperature</Tag><Tag>Water</Tag><Tag>Fish</Tag><Tag>
<ards xmlns:ard="http://www.ict-sensei.org/AdvancedResourceDescription/" 
xmlns:sweet="http://sweet.jpl.nasa.gov/ontology/property.owl#" 
xmlns:wn="http://purl.org/vocabularies/princeton/wn30/" 
xmlns:gn="http://www.geonames.org/ontology#"
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<ard:Resource-Description Name="Fish pond sensor 1" ResourceId="http://m2m.ericsson.com/sensors/causensor010">
  <ard:location>
<gn:name>CAU_Lab_Aquarium</gn:name>
<geo:lat>40.022464</geo:lat>
<geo:long>116.27692</geo:long>
<geo:alt>11.27</geo:alt>
</ard:location>

  <ard:Expiration-Time>12-04-21T15:02:30</ard:Expiration-Time>
  <ard:Operation-Description>
    <ard:input>report data</ard:input>
    <ard:output>
      <ard:information-type>
        <Property>
          <Unit>degreeC</Unit>
          <Name>temperature</Name>
        </Property>
        <Feature>water</Feature>
      </ard:information-type>
    </ard:output>
  </ard:Operation-Description>
</ard:Resource-Description>
</ards>
</Tag><RAI-Description><RAI-ID></RAI-ID><Description>POST SQL query/GET directly</Description><Document-Link>urn:sensei:rai:dh:dsqs6</Document-Link><REP-Locator Expiration-Time="12-04-21T15:02:30+00:00"></REP-Locator></RAI-Description></Resource-Description></Result>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics