如何解析XML字符串

时间:2014-02-17 15:33:02

标签: php mysql xml

我是Php的新手,想在我的PhpMyAdmin数据库中解析表中的XML响应,解析后我想将响应插入到不同的表中。

正在处理的XML字符串示例如下:

<?xml version="1.0" encoding="utf-8" ?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:sendSmsResponse xmlns:ns1="http://www.csapi.org/schema/parlayx/sms/send/v2_2/local"><ns1:result>1000022003011402

我研究过如何通过php解析XML字符串,上面的XML字符串可以解析如下:

<?php

$xml=simplexml_load_file("<?xml version="1.0" encoding="utf-8" ?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:Response xmlns:ns1="http://www.csapi.org/schema/parlayx/sms/send/v2_2/local"><ns1:result>1000022003011402;
");
echo $xml->result() . "<br>";

foreach($xml->result() as $delivery_result)
  {
  echo $delivery_result->getResult() . ": " . $delivery_result . "<br>";
  }

?> 

但我坚持如何解析我的PhpAdmin数据库中的众多字符串,并将结果插入到差异表中,只需指向正确的方向,我相信我会到达那里。

这是登录我的数据库的方式:

#set the connection

my $dbh =DBI->connect('DBI:mysql:NBKANGO', 'Kango', 'XXXXXXXXX') || die "Could not connect to database: $DBI::errstr";

1 个答案:

答案 0 :(得分:0)

如果你想在mysql中做,你可以尝试像

这样的东西
SELECT SUBSTRING(field,LOCATE('<ns1:result>', field),resultlength);