来自XML的打印值响应

时间:2016-01-21 11:52:44

标签: php

我正在使用网络服务。我发送请求并获取xml作为响应。 这就是我发送的内容:

$s = new soapclient($conDetails['url'],array('wsdl'));
$params = new stdClass;
$paramsStr = '
            <GetPackItinerary UserID="'.$conDetails['UsrId'].'" SessionID="'.$conDetails['Session'].'" >
                <root>
                    <ItinId>'.$_GET['int'].'</ItinId>
                </root>
            </GetPackItinerary>
            ';
$params->xmlRequest = $paramsStr;

$result = $s->__call("SubmitXmlString",array($params));

如果我打印我的resualt看起来像那样:

<GetPackItinerary Cnt="1">
    <int id="39">
        <header>
            <ItinId>39</ItinId>
            <Name>text for tour</Name>
            <Class>STD</Class>
            <Days>10</Days>
            <Text/>
            <Include>text for tour</Include>
            <NotInclude>text for tour</NotInclude>
            <Url>http://www.geotours.co.il</Url>
            <Status>OK</Status>
        </header>
        <day id="1">
            <ItinId>39</ItinId>
            <Destination/>
            <Day>1</Day>
            <Meal/>
            <Header>text for day 1</Header>
            <Text>some text</Text>
            <Include>some text</Include>
            <NotInclude/>
        </day>
        <day id="2">
            <ItinId>39</ItinId>
            <Destination/>
            <Day>2</Day>
            <Meal/>
            <Header>text for day 2</Header>
            <Text>some text 2</Text>
            <Include>some text 2</Include>
            <NotInclude/>
        </day>
    </int>
</GetPackItinerary>

我的问题是 - 我究竟从WS回来了什么?是XML吗? obeject? 并且 - 如何打印一些值,即时 - 标签&#34; name&#34;在&#34;标题&#34;标签(旅游文本)?

2 个答案:

答案 0 :(得分:0)

对我来说似乎是“XML”。您可以使用SimpleXML访问XML的元素。这是一种方式: -

<?php
//generate a file from **$result** and store it in your directory.
//save it as myfile.xml, which would be handled by simplexml_load_file
$fileHandler = simplexml_load_file("myfile.xml");
echo $fileHandler->GetPackItinerary->int->header->Name;

//outputs :- text for tour
?>

答案 1 :(得分:0)

使用标准DOMDocument一旦你了解了一些基础知识,就很容易在各个节点中导航,但下面的内容应该给你一般的想法。

$strxml='<GetPackItinerary Cnt="1">
            <int id="39">
                <header>
                    <ItinId>39</ItinId>
                    <Name>text for tour</Name>
                    <Class>STD</Class>
                    <Days>10</Days>
                    <Text/>
                    <Include>text for tour</Include>
                    <NotInclude>text for tour</NotInclude>
                    <Url>http://www.geotours.co.il</Url>
                    <Status>OK</Status>
                </header>
                <day id="1">
                    <ItinId>39</ItinId>
                    <Destination/>
                    <Day>1</Day>
                    <Meal/>
                    <Header>text for day 1</Header>
                    <Text>some text</Text>
                    <Include>some text</Include>
                    <NotInclude/>
                </day>
                <day id="2">
                    <ItinId>39</ItinId>
                    <Destination/>
                    <Day>2</Day>
                    <Meal/>
                    <Header>text for day 2</Header>
                    <Text>some text 2</Text>
                    <Include>some text 2</Include>
                    <NotInclude/>
                </day>
            </int>
        </GetPackItinerary>';

$dom=new DOMDocument;
$dom->loadXML( $strxml );

$root=$dom->getElementsByTagName('GetPackItinerary')->item(0);
$header=$dom->getElementsByTagName('header')->item(0);
$days=$dom->getElementsByTagName('day');

$tour=$header->childNodes->item(3);

echo $tour->tagName.': '.$tour->nodeValue.BR;/* Name: text for tour */

foreach( $days as $node ){/* 1,3,5,7,9 etc ~ even numbers are textNodes! */
    echo $node->childNodes->item(9)->tagName.': '.$node->childNodes->item(9)->nodeValue.BR;
}