我不能用PHP解析XML

时间:2018-05-21 02:53:49

标签: php xml

我有这样的代码将数据从ajax调用返回到这个php文件:

$db = new PDO('mysql:host=localhost;dbname=DB', 'root', '');
    $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
    $stmt = $db->prepare('SELECT XML FROM `table` WHERE `ID`="1";');
    $stmt->execute();
    $result = $stmt->fetchAll();

现在在$ result中我有XML文件,其结构如下:

<Report xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='Report' xsi:schemaLocation='Report http://127.0.01/' Name='Report' Customer='Data'><Tablix1><Textbox1/><DialedNumber_Collection><DialedNumber DialedNumber1='3468452864'><Details_Collection><Details DialingNumber='34647991111' StartTime='2018-05-02T08:09:28' HangupTime='2018-05-02T08:09:39.957' WaitDuration='11' isUserInformed='True'/><Details DialingNumber='34647991111' StartTime='2018-05-02T08:18:13' AnswerTime='2018-05-02T08:18:40.547' DialedNumbers='123' AnswerNumber='123' HangupTime='2018-05-02T08:20:07.417' ConversationDuration='87' WaitDuration='27'/><Details DialingNumber='3453455626' StartTime='2018-05-02T08:22:17' AnswerTime='2018-05-02T08:22:53.56' DialedNumbers='104' AnswerNumber='104' HangupTime='2018-05-02T08:24:03.067' ConversationDuration='70' WaitDuration='36'/><Details DialingNumber='345394356' StartTime='2018-05-02T08:29:50' AnswerTime='2018-05-02T08:30:23.17' DialedNumbers='106;159' AnswerNumber='159' HangupTime='2018-05-02T08:31:32.563' ConversationDuration='69' WaitDuration='33'/><Details DialingNumber='34534881231' StartTime='2018-05-02T09:16:57' AnswerTime='2018-05-02T09:17:32.17' DialedNumbers='106;159' AnswerNumber='159' HangupTime='2018-05-02T09:18:55.41' ConversationDuration='83' WaitDuration='35'/><Details DialingNumber='234343245435' StartTime='2018-05-02T10:01:25' DialedNumbers='104' HangupTime='2018-05-02T10:01:55.73' WaitDuration='30' isUserInformed='True'/><Details DialingNumber='3453453456' StartTime='2018-05-02T10:25:48' AnswerTime='2018-05-02T10:26:18.24' DialedNumbers='104' AnswerNumber='104' HangupTime='2018-05-02T10:26:58.307' ConversationDuration='40' WaitDuration='30'/><Details DialingNumber='435345343245' StartTime='2018-05-02T10:41:58' AnswerTime='2018-05-02T10:42:28.933' DialedNumbers='104' AnswerNumber='104' HangupTime='2018-05-02T10:43:21.657' ConversationDuration='53' WaitDuration='30'/><Details DialingNumber='23423465676' StartTime='2018-05-02T10:46:22' AnswerTime='2018-05-02T10:46:54.163' DialedNumbers='104' AnswerNumber='104' HangupTime='2018-05-02T10:47:19.257' ConversationDuration='25' WaitDuration='32'/><Details DialingNumber='45342625634' StartTime='2018-05-02T11:16:24' HangupTime='2018-05-02T11:16:39.2' WaitDuration='15'/><Details DialingNumber='2342343456' StartTime='2018-05-02T11:32:33' DialedNumbers='104' HangupTime='2018-05-02T11:33:24.123' WaitDuration='51' CallBackTime='2018-05-02T11:34:19' CallBackNumber='23423452344' CallBackShortNumber='104' isUserInformed='True'/><Details DialingNumber='24255550' StartTime='2018-05-02T12:22:08' DialedNumbers='104' HangupTime='2018-05-02T12:22:56.8' WaitDuration='48' CallBackTime='2018-05-02T13:22:10' CallBackNumber='2342353404' CallBackShortNumber='104' isUserInformed='True'/><Details DialingNumber='23423459806' StartTime='2018-05-02T12:39:53' DialedNumbers='110' HangupTime='2018-05-02T12:40:25.373' WaitDuration='32' isUserInformed='True'/><Details DialingNumber='234234541' StartTime='2018-05-02T13:01:20' AnswerTime='2018-05-02T13:01:58.763' DialedNumbers='104' AnswerNumber='104' HangupTime='2018-05-02T13:04:35.463' ConversationDuration='157' WaitDuration='38'/><Details DialingNumber='345654772' StartTime='2018-05-02T13:30:50' AnswerTime='2018-05-02T13:31:01.457' DialedNumbers='128;116' AnswerNumber='128' HangupTime='2018-05-02T13:31:29.483' ConversationDuration='28' WaitDuration='11'/><Details DialingNumber='234234238' StartTime='2018-05-02T13:52:08' AnswerTime='2018-05-02T13:53:25.017' DialedNumbers='110' AnswerNumber='110' HangupTime='2018-05-02T14:01:26.58' ConversationDuration='481' WaitDuration='77'/><Details DialingNumber='37061449825' StartTime='2018-05-02T16:15:54' AnswerTime='2018-05-02T16:16:25.45' DialedNumbers='128;116' AnswerNumber='128' HangupTime='2018-05-02T16:18:12.617' ConversationDuration='107' WaitDuration='31'/><Details DialingNumber='234234234' StartTime='2018-05-02T16:31:57' AnswerTime='2018-05-02T16:32:19.097' DialedNumbers='106;159' AnswerNumber='159' HangupTime='2018-05-02T16:32:55.83' ConversationDuration='36' WaitDuration='22'/></Details_Collection></DialedNumber></DialedNumber_Collection></Tablix1></Report>

我尝试使用以下代码获取(解析)DialingNumber,ConversationDuration和StartTime:

$array = json_decode(json_encode($result), true); // Convert to array

print_r($array); // Just to see data structure i will delete it in future
echo $array->ReportData->DialingNumber; 



$ids = $array->ReportData->DialingNumber;
foreach($ids as $id) {
    echo $id;
}

或者如何从XML输出/回显这样的数据结构?我会理解如何完成它。

echo $array->ReportData->DialingNumber; //I dont understand how correctly to echo it

1 个答案:

答案 0 :(得分:1)

获取价值的一个选择是使用SimpleXMLElement

DialingNumber,ConversationDuration和StartTime是attributes的一部分。

        {
        Console.Write("Hur stor kvadrat vill du ha? ");
        int size = Convert.ToInt32(Console.ReadLine());

        for (int y = 0; y < size; y++)
        {
            for (int x = 0; x < size; x++)
            {
                Console.Write("*");
            }
            Console.WriteLine("");
        }
        Console.ReadKey();
    } code here