六、撰写文档与编写测试用例

本周的的代码方面的工作比较少,主要是编写一个测试用例,然后开始撰写相关的文档

中期报告提到,我们调用OData数据服务返回的结果是xml格式的文档,那么我们的测试用例就应该对其进行解析,这里我们采用了SimpleXml这个类

原理就是讲返回的xml文档解析成一个xml对象,然后处理其中的属性和内容

<?php
	$contents = file_get_contents("http://127.0.0.1/SugarCRM.svc/Contacts(id='1250c7bd-6664-6a31-b96f-53c156b9ef7e')");
	//print_r($contents);
	/*$xml = simplexml_load_string($contents);
	print_r($xml);
	$m_elements   = $xml->children('m', TRUE);
	$m_properties = $m_elements->properties;
	$d_elements   = $m_properties->children('d', TRUE);*/
	$x = new SimpleXmlElement($contents); 
	foreach($x->entry as $t){ 
		foreach ($t->content as $con) {
 
			$namespaces = $con->getNameSpaces(true); 
			$m = $con->children($namespaces['m']);
			$m_properties = $m->properties;
			$namespaces1 = $m_properties->getNameSpaces(true); 
			$d = $m_properties->children($namespaces1['d']);
		
			echo $d->id; 
			echo $d->first_name; 
			echo $d->last_name;
		}
	}
	/*echo $t->id . "<br >"; 
	echo $t->updated . "<br />"; 
	$namespaces = $t->getNameSpaces(true); 
	$gd = $t->children($namespaces['gd']); 
	echo $gd->phoneNumber; 
*/
	//echo $d_elements->id;
?>

其中首先打开数据服务的url,获取其中内容,然后建立一个xml对象,并逐级进行解析

这里与一般的xml不同的是需要对命名空间同时进行解析,就是其中的getNameSpacs函数,按照命名空间解析之后即可返回数据。

然后撰写文档的内容基本与之前的博客相同,在此就不再进行赘述。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章