2013-10-08 125 views
0

好日子我正尝试将xml发送到Web服务,但需要在发送前清除xml。到目前为止,我尝试了不同的方式,现在已经停滞了一段时间。如何在发送到Web服务之前清理xml请求

我从表单中捕获数据并将其发布到我的php文件中进行处理。如果用户没有输入长度/宽度/高度的任何数据,那么我想清理我的xml并删除空元素,以便它可以在发送xml请求的服务器上通过验证。

下面是我的文章中的数据cpatured的一个片段,并相应地构建xml文件,但如果这些维度被忽略会怎么样?我还可以清理其他空的元素吗?

$xmlRequest = <<<XML 
<?xml version="1.0" encoding="UTF-8"?> 
<mailing-scenario xmlns="http://www.mysite.com/ws/ship/rate-v2"> 
<customer-number>{$mailedBy}</customer-number> 
    <parcel-characteristics> 
    <weight>{$weight}</weight> 
     <dimensions> 
     <length>{$length}</length> 
     <width>{$width}</width> 
     <height>{$height}</height> 
     </dimensions> 
    </parcel-characteristics> 
<origin-postal-code>{$originPostalCode}</origin-postal-code> 
<destination> 
<domestic> 
<postal-code>{$postalCode}</postal-code> 
</domestic> 
</destination> 
</mailing-scenario> 
XML; 


$xmlRequest = phpquery::newDocument(); 
$xp = new DOMXPath($xmlRequest->getDOMDocument()); 
foreach($xp->query('//*[not(node()) or normalize-space() = ""]') as $node) { 
$node->parentNode->removeChild($node); 
} 
+0

我不确定你的意思是干净的。如果您使用php构建XML文件,只需检查提供的用户数据是否有效。如果你需要清理它,则构建另一个XML版本。 如果你的XML已经解析了所有节点!并检查它们是否有效。 – mjb4

+0

感谢我对此新。我已经构建了如上所示的xml,但如果用户未输入长度,宽度,高度,我如何去除维度元素。我如何重建另一个XML并省略空的?谢谢 – user2859027

+0

我从来没有看过这个XML查询的东西,从PHP需要做到这一点。但试试看[文档](http://php.net/manual/de/class.domxpath.php)。 你只需要搜索方式来查找像宽度类型的节点,然后检查它的值,如果无效删除它,或者它的父母或父母的父母... – mjb4

回答

0

好的这里有一个简单的dom例子。也许有些观点首先,如果没有给出顾客号码或负面权重,你将决定怎么做......。
所以你必须清理XML,但有时清理它会使请求无效,或者用户可能会得到他没有想到的结果。例如,他可能会将1kg设为权重,因此您删除kg,因为权重设置为g,并且字符串存在错误。如果你不告诉用户,他可能会骂你!
而且也只是因为所有节点都是有效的并不意味着请求是正确的,因为可能有一些缺少节点,所以你也必须检查需求!

效率的最后一个字,如果您可以在没有XML的情况下从用户那里获取所有这些字段,因为用户每次只发送一个宗地。这样做,只是检查数据是否正确。
如果您必须使用XML,那么每次只需发送一个包,您只需获取数据即可检查数据的有效性并重新构建经过验证的XML。

我只是使用这个例子,如果我知道这些XML请求可能是非常广泛的和/或有一个复杂的格式。

function cleanXML($data){ 
    // ok the data is string! So get your node. 
    $node = simplexml_load_string($data); 

    // now we can iterate throught all child nodes: 
    foreach($node->children() as $child){ 
     //so here we got the childrens 

     // All child nodes of the root should be mailing scenarios 
     if($child->getName() == "mailing-scenario"){ 
      //otherwise we check if mailing scenario is valid 
      if(!validateMScenario($child)){ 
       //This node seems not so valid 
       //you have to decide what to do now! 
      } 
     } 
     else{ 
      //Here we remove all nodes that are different 
      unset($child[0]); 

      echo "Error: Needed to remove Node"; 
     } 
    } 

    // Just give them their cleaned XML! 
    return $node->asXML(); 
} 


function validateMScenario($ms){ 
    // These var's safe if the requirements are fullfilled 
    $is_customer_number_set = 0 
    $is_weight_set = 0 
    $is_package_set = 0 

    // Again iterate over nodes 
    foreach($ms->children as $child){ 

     //check for customer number 
     if($child->getName() == "customerNumber"){ 
      if($is_customer_number_set == 1){ 
       echo "You just need one customer number I guess?!" 
       return -1 
      } 

      value = (string) $child; 

      // Check if customer number is existing 
      if(strlen(value) == 0 || !is_int(value) || intval(value) == -1){ 
       echo "Dude you need a number!"; 
       return -1 
      } 

      $is_customer_number_set = 0; 
     } 
     else if($node->getName() == "parcel-characteristics"){ 
      //Ok I hope it should be pretty clear what to do here! 
      //... 
     } 
     else{ 
      //Remove node again? 
     } 
    } 

    // All requirements fullfilled? 
    return ($is_customer_number_set && $is_weight_set && $is_package_set); 
} 
相关问题