getMessage()); } try { //获取字符串 转成数组 $xml_string = 'your_xml_string'; // $xml_string = ""; $result = ServiceSchemaReader::readXmlForArrayByString($xml_string); echo var_dump($result), PHP_EOL; //将处理后的数组转为最终结果 $res = ServiceSchemaWriter::writeSchemaXmlString(handleResult($result)); echo var_dump($res), PHP_EOL; } catch (Exception $e) { var_dump($e->getMessage()); } function handleResult($result){ $attributeArr = array(); foreach ($result as $key => $value) { switch ($key){ case "serviceName": $value->setValues("sdk构造测试"); break; case "serviceDesc": $value->setValues("sdk构造测试-描述"); break; case "serviceUrl": foreach ($value->getValues() as $item){ foreach ($item->getValues() as $attr) { if($attr->getId() == 'carrierType'){ $attr->setValues('SC_MINI_APP'); }elseif ($attr->getId() == 'carrierUrl'){ $attr->setValues('alipays://platformapi/startapp?appId=2019101468340048&page=pages'); } } } break; case "poi": foreach ($value->getValues() as $attr){ if($attr->getId() == 'poiName'){ $attr->setValues('肯德基'); }elseif ($attr->getId() == 'address'){ $attr->setValues('蚂蚁金服A空间'); }elseif ($attr->getId() == 'cityCode'){ $attr->setValues('330100'); } } break; case "A2021031200406612": $value->setValues(array("6","5")); break; } $attributeArr[] = $value; } return $attributeArr; } //single $singleAttribute = setSingleAttribute(); //$singleAttribute = setSingleAttribute("errValue"); //multi $multiAttributes = setMultiAttribute(); //complex $complexAttributes = setComplexAttribute(); //multiComplex $multiComplexAttributes = setMultiComplexAttribute(); //数组构建schema的xml格式(不含rules和options) try { echo var_dump(ServiceSchemaWriter::writeSchemaXmlString(array($singleAttribute, $multiAttributes, $complexAttributes, $multiComplexAttributes))), PHP_EOL; } catch (Exception $e) { var_dump($e->getMessage()); } //数组构建schema的xml格式(完整格式) try { echo var_dump(ServiceSchemaWriter::writeFullchemaXmlString(array($singleAttribute, $multiAttributes, $complexAttributes, $multiComplexAttributes))), PHP_EOL; } catch (Exception $e) { var_dump($e->getMessage()); } function setSingleAttribute($errType=null) { $attribute = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "single", "text"); //构造异常数据 if($errType=='errId'){ $attribute = ServiceSchemaFactory::createAttribute(null, "服务名称", "single", "text"); }elseif ($errType=='errValueType'){ $attribute = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "single", "text123"); } //values $attribute->setValues("服务提报demo"); if($errType=='errValue'){ $attribute->setValues(array("服务提报demo")); } //rules $rules = array( $errType == 'errRule' ? ServiceSchemaFactory::createRule("required", "必填") : ServiceSchemaFactory::createRule("required", "必填", "true"), ServiceSchemaFactory::createRule("maxLength", "最大长度", "15") ); $attribute->setRules($rules); //options $options = array( $errType == 'errOption' ? ServiceSchemaFactory::createOption("支付宝小程序") : ServiceSchemaFactory::createOption("支付宝小程序", "SC_MINI_APP"), ServiceSchemaFactory::createOption("高德小程序", "AMAP_MINI_APP") ); $attribute->setOptions($options); return $attribute; } function setMultiAttribute($errType=null) { $attribute = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "multi", "text"); //values $values = array("周一", "周二"); $attribute->setValues($values); if($errType=='errValue'){ $attribute->setValues("周一"); } //rules $rules = array( ServiceSchemaFactory::createRule("pattern", "正则表达式", "[\\u4E00-\\u9FA5A-Za-z0-9_+\\-()()]+"), ServiceSchemaFactory::createRule("minLength", "最小长度", "2") ); $attribute->setRules($rules); //options $options = array( ServiceSchemaFactory::createOption("UC小程序地址", "UC_MINI_APP"), ServiceSchemaFactory::createOption("夸克小程序地址", "QUARK_MINI_APP") ); $attribute->setOptions($options); return $attribute; } function setComplexAttribute($errType=null) { $attributes = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "complex", "text"); $singleAttribute = setSingleAttribute(); $multiAttributes = setMultiAttribute(); //values $attributes->setValues(array($singleAttribute, $multiAttributes)); if($errType=='errValue'){ $attributes->setValues("服务提报demo"); } $attributes->setRules(array( ServiceSchemaFactory::createRule("pattern", "正则表达式", "[\\u4E00-\\u9FA5A-Za-z0-9_+\\-()()]+"), ServiceSchemaFactory::createRule("minLength", "最小长度", "2") )); return $attributes; } function setMultiComplexAttribute($errType=null) { $attribute = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "multiComplex", "text"); $attributesComplex = setComplexAttribute(); //values $values = array($attributesComplex, $attributesComplex); $attribute->setValues($values); if($errType=='errValue'){ $attribute->setValues(array("服务提报demo")); } return $attribute; }