xml和php - 输出文本

时间:2013-02-12 09:23:22

标签: php xml xml-parsing

我需要快速帮助阅读我的xml。我在工具提示中显示我的结果,一切准备就绪。我想逐个id地查询每个批次。我需要一个PHP代码来为他们id的每个批次 这是我的XML

<section id="8" number="8" name="Section 8" phase="4" date="2/11/2013">
<lot id="2198" lot="2" block="1" section="8">
<number>2</number>
<legal>L2-B1-S8 (60')</legal>
<address>12107 Arbor Blue Lane</address>
<builder>Perry Homes</builder>
<status>Sold</status>
<plan>3158</plan>
<brick>Acme-StoneBriar</brick>
<trim>SW-Portbello, Tea Chest</trim>
<final_inspection_date/>
<completion>5/23/2013</completion>
<sqft>3158</sqft>
<stories>2</stories>

<bed>4</bed>
<bath>3</bath>
<garage>3</garage>
<lotwidth>60'</lotwidth>
<specprice>Sold</specprice>
<imglocation>none</imglocation>
<premium>off</premium>
</lot>
<lot id="2218" lot="22" block="1" section="8">
<number>22</number>
<legal>L22-B1-S8 (60')</legal>
<address>19503 Bella Arbor Lane</address>
<builder>Perry Homes</builder>
<status>Sold</status>
<plan>3391</plan>
<brick>Hanson-Chestnut Hill</brick>
<trim>SW-Virtual Taupe, Smokehouse</trim>
<final_inspection_date>12/3/2012</final_inspection_date>
<completion>11/28/2012</completion>
<sqft>3391</sqft>
<stories>2</stories>
<bed>4</bed>
<bath>3</bath>
<garage>3</garage>
<lotwidth>60'</lotwidth>
<specprice>Sold</specprice>
<imglocation>3391w-e51.png</imglocation>
<premium>off</premium>
</lot>
</section>

这是我的测试PHP代码

    $xmldoc = new DOMDocument();
    $xmldoc->load('mydata.xml');

    $xpathvar = new Domxpath($xmldoc);

    $queryResult = $xpathvar->query('//lot[@id="2198"]/address'); 
    foreach($queryResult as $result){
            echo $result->textContent;

    }

这只是给了我一个地址。如何列出所有其他属性?我现在可以在我的工具提示中使用这个

    <div style="float:left">

<p style="
    margin: 0;
    padding-left: 10px;
    font-size: 13px;
"><label style="color:grey">Status: </label>'.$status.'</p>
<p style="
    margin: 0;
    padding-left: 10px;
    font-size: 13px;
"><label style="color:grey">Builder: </label>'.$builder.'</p>
<p style="
    margin: 0;
    padding-left: 10px;
    font-size: 13px;
    width:200px
"><label style="color:grey">Address: </label>'.$address.'</p>
<p style="
    margin: 0;
    padding-left: 10px;
    font-size: 13px;
"><label style="color:grey">Square Ft: </label>'.$square.'</p>
<p style="
    margin: 0;
    padding-left: 10px;
    font-size: 13px;
"><label style="color:grey">Stories: </label>'.$stories.'</p>

<p style="
    margin: 0;
    padding-left: 10px;
    font-size: 13px;
"><label style="color:grey">Completion: </label>'.$completion.'</p>


<p style="
    margin: 0;
    padding-left: 10px;
    font-size: 13px;
"><label style="color:grey">Plan #: </label>'.$plan.'</p>
<p style="
    margin: 0;
    padding-left: 10px;
    font-size: 13px;
"><label style="color:grey">B/B/G: </label>'.$bed.'/'.$bath.'/'.$garage.'</p>
<p style="
    margin: 0;
    padding-left: 10px;
    font-size: 13px;
"><label style="color:grey">Spec Price: </label>'.$specprice.'</p>


</div>

1 个答案:

答案 0 :(得分:0)

//lot[@id="2198"]/address

你问:“这只是给了我地址。如何列出所有其他属性?”

在Xpath中,如果您不想指定元素的具体名称,则可以使用星号*

//lot[@id="2198"]/*

示例:ZVON Example 3: The star * selects all elements located by preceeding path

但你的问题对我来说并不是特别清楚,所以这个答案有点猜测。如果不能帮助你,请不要怪我;)