这是我用来在admin_xml.php
中发送XML数据的简单表单<form name="review" action="admin_xml.php" method="post">
<textarea name="xml" cols="40" rows="10"></textarea>
<input class="submit" type="submit" value="Submit Request">
</form>
这是我输入的XML,用于从MySQL数据库中检索数据
<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersIds>
<Credentials>
<Username>my_username</Username>
<Password>my_password</Password>
</Credentials>
<Criterions>
<OrderNumber></OrderNumber>
<StartDate>2009-01-01</StartDate>
<EndDate>2009-07-01</EndDate>
</Criterions>
</GetOrdersIds>
这是从xml中提取标签的php代码:
<?php
$text_all = "<?xml version=\"1.0\"?>\r\n<GetOrdersIds version=\"1.0\">\r\n<Credentials>\r\n<Username>my_username</Username>\r\n<Password>my_password</Password>\r\n</Credentials>\r\n<Criterions>\r\n<OrderNumber></OrderNumber>\r\n<StartDate>2009-01-01</StartDate>\r\n<EndDate>2009-07-01</EndDate>\r\n</Criterions>\r\n</GetOrdersIds>";
$field = "Criterions";
$result = substr($text_all, strpos($text_all, "<".$field.">")+strlen("<".$field.">"), strpos($text_all, "</".$field.">")-strlen("<".$field.">")-strpos($text_all, "<".$field.">"));
?>
以上php代码的结果是:
<OrderNumber></OrderNumber>
<StartDate>2009-01-01</StartDate>
<EndDate>2009-07-01</EndDate>
当脚本运行时,php代码会遍历我的mysql数据,并提取上面给出的开始日期和结束日期之间的所有订单。
是否有更好的方法来改进以下PHP代码,以便它执行相同的功能:
$result = substr($text_all, strpos($text_all, "<".$field.">")+strlen("<".$field.">"), strpos($text_all, "</".$field.">")-strlen("<".$field.">")-strpos($text_all, "<".$field.">"));
这是搜索xml数据并检索所有标记的代码。
更新:
任何人都可以在SimpleXML中列出$ result代码吗?
答案 0 :(得分:3)
一些临时变量会有所帮助,也更容易阅读。
$result = substr($text_all, strpos($text_all, "<".$field.">")+strlen("<".$field.">"), strpos($text_all, "</".$field.">")-strlen("<".$field.">")-strpos($text_all, "<".$field.">"));
可以改写为。
$tagLen = strlen('<'.$field.'>');
$openTag = strpos($text_all, '<'.$field.'>');
$closeTag = strpos($text_all, '</'.$field.'>', $openTag);
$result = substr($text_all, $openTag + $tagLen, $closeTag - $tagLen - $openTag);
或使用SimpleXML
$doc = simplexml_load_string($text_all);
echo $doc->Criterions->asXML();
如果您希望单个值使用此值。
$doc = simplexml_load_string($text_all);
echo $doc->Criterions->OrderNumber;
echo $doc->Criterions->StartDate;
echo $doc->Criterions->EndDate;
如果你想要XML格式的元素,那么使用asXML():
$doc = simplexml_load_string($text_all);
echo $doc->Criterions->OrderNumber->asXML();
echo $doc->Criterions->StartDate->asXML();
echo $doc->Criterions->EndDate->asXML();
答案 1 :(得分:2)
使用SimpleXML:
<?php
$xmlSTR = '<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersIds>
<Credentials>
<Username>my_username</Username>
<Password>my_password</Password>
</Credentials>
<Criterions>
<OrderNumber></OrderNumber>
<StartDate>2009-01-01</StartDate>
<EndDate>2009-07-01</EndDate>
</Criterions>
</GetOrdersIds>';
$xml = new SimpleXMLElement($xmlSTR);
echo $xml->Credentials->Username;
//To see the entire structure print_r($xml); as php can access objects as arrays
答案 2 :(得分:1)
查看simplexml模块。
答案 3 :(得分:1)
使用像SimpleXML这样的XML解析器来提取数据:
$doc = simplexml_load_string($_POST['xml']);
$OrderNumber = $doc->Criterions->OrderNumber;
$StartDate = $doc->Criterions->StartDate;
$EndDate = $doc->Criterions->EndDate;
答案 4 :(得分:1)
假设您的XML字符串存储在名为$text_all
的变量中(如您在示例中所示),这将为您提供您想要的人...: - )
$mysql_stuff = '';
$xml = new SimpleXMLElement($text_all);
foreach($xml->Criterions->children() as $criterion) {
$mysql_stuff .= $criterion->asXML()."\n";
}
echo $mysql_stuff;
$mysql_stuff
现在将包含:
<OrderNumber/>
<StartDate>2009-01-01</StartDate>
<EndDate>2009-07-01</EndDate>
我测试了这个并且它有效。我希望这回答了你的问题!