有个jt.xml文件,utf-8格式,内容如下<root>
  <SecondHouse id="217045" userid="jtzy" password="123456" del="0">
  <House>3-2-2</House>
  <AddDate>2010-11-2 16:35:08</AddDate>
  </SecondHouse>
  <SecondHouse id="217042" userid="jtzy" password="123456" del="0">
  <House>3-2-1</House>
  <AddDate>2010-11-2 16:30:56</AddDate>
  </SecondHouse>
  <SecondHouse id="217041" userid="jtzy" password="123456" del="0">
  <House>2-2-1</House>
  <AddDate>2010-11-2 16:19:06</AddDate>
  </SecondHouse>
</root>现需用php代码把此页面中的 House AddDate 这二个字段内容放入二维数组中,如何实现呀,thanks

解决方案 »

  1.   


    <?php
    $xml = simplexml_load_file('jt.xml');//文件方式
    foreach($xml->SecondHouse as $SecondHouse){
        $House[] = (array)$SecondHouse->House;
        $AddDate[] = (array)$SecondHouse->AddDate;    
    }
    ?>$House[0]和$AddDate[0]就是你要的结果!
      

  2.   

    我的 php 不支持 simplexml_load_file 这个函数,有没有什么方法 让我可用这个函数?
      

  3.   

    开启domxml扩展!如果不用simplexml_load_file,也可以用正则
      

  4.   


    <?php
    $xml = file_get_contents('jt.xml');
    preg_match_all("/.*\<House\>\d+\-\d+\-\d+\<\/House\>.*/",$xml,$House);
    print_r($House[0]);
    ?>AddDate可参考House
      

  5.   

    6楼的,
    16楼的可运行,可我想把  hosue 之间的不用 d+ d+ 的,想设置成 * 上面的怎么改呀,
    2我是linux系统中,在哪个文件中开启domxml扩展呀,
      

  6.   

    1.\d+改成\d*2.php 的配置文件!
      

  7.   

    楼上的,
    1 为何我换了下面的一段xml内容就运行不了,格式还是utf-8,如何才能运行下面的xml内容
    <root>
    <SecondHouse id="217155" userid="jtzy" password="123456" del="0">
      <City>南昌</City>
      <Area>莲塘</Area>
      <BH>XXH217155</BH>
      <Simple>澄湖北路梦里水乡</Simple>
      <House>2-1-1</House>
      <Number></Number>
      <Kind>商品房</Kind>
      <Floor>6/4</Floor>
      <Sunny>南北</Sunny>
      <BuildArea>101.96</BuildArea>
      <BuildTime></BuildTime>
      <Fitment>简装</Fitment>
      <FitmentTime>0</FitmentTime>
      <Layout></Layout>
      <Bround></Bround>
      <ManagerPrice></ManagerPrice>
      <Price>50</Price>
      <UnitPrice></UnitPrice>
      <Note></Note>
      <Tel>0791-7186688;7160778;7572099</Tel>
      <Linkman>信息部</Linkman>
      <AddDate>2010-11-3 13:30:03</AddDate>
      </SecondHouse>
      <SecondHouse id="217152" userid="jtzy" password="123456" del="0">
      <City>南昌</City>
      <Area>红谷滩</Area>
      <BH>TYS217152</BH>
      <Simple>绿茵路红谷世纪花园</Simple>
      <House>2-2-1</House>
      <Number></Number>
      <Kind>商品房</Kind>
      <Floor>11/3</Floor>
      <Sunny>南北</Sunny>
      <BuildArea>102</BuildArea>
      <BuildTime></BuildTime>
      <Fitment>精装</Fitment>
      <FitmentTime>0</FitmentTime>
      <Layout></Layout>
      <Bround></Bround>
      <ManagerPrice></ManagerPrice>
      <Price>85</Price>
      <UnitPrice></UnitPrice>
      <Note></Note>
      <Tel>6530038/15270916366</Tel>
      <Linkman>桃苑店</Linkman>
      <AddDate>2010-11-3 12:14:01</AddDate>
      </SecondHouse>
      <SecondHouse id="217151" userid="jtzy" password="123456" del="0">
      <City>南昌</City>
      <Area>青山湖区</Area>
      <BH>TYS217151</BH>
      <Simple>解放西路国药厂宿舍</Simple>
      <House>2-1-1</House>
      <Number></Number>
      <Kind>商品房</Kind>
      <Floor>7/7</Floor>
      <Sunny>南北</Sunny>
      <BuildArea>68</BuildArea>
      <BuildTime></BuildTime>
      <Fitment>简装</Fitment>
      <FitmentTime>0</FitmentTime>
      <Layout></Layout>
      <Bround></Bround>
      <ManagerPrice></ManagerPrice>
      <Price>34</Price>
      <UnitPrice></UnitPrice>
      <Note>另送一个8平米的储藏间</Note>
      <Tel>6530038/15270916366</Tel>
      <Linkman>桃苑店</Linkman>
      <AddDate>2010-11-3 12:11:49</AddDate>
      </SecondHouse>
    </root>
      

  8.   

    测试过了~~正则和simplexml_load_file都没问题啊~simplexml_load_file你确保下你读取的xml文件是utf-8编码!切忌东西不要照抄啊,自己多理解!
      

  9.   

     <AddDate>2010-11-3 12:11:49</AddDate>如何取 <AddDate>  </AddDate> 之间的值呀, 
      

  10.   

    真难伺候啊!
    preg_match_all("/.*\<AddDate\>.*<\/AddDate\>.*/",$xml,$AddDate);
    print_r($$AddDate[0]);
      

  11.   

    preg_match_all("/.*\<AddDate\>.*\<\/AddDate\>.*/",$xml,$AddDate);
    print_r($$AddDate[0]);
      

  12.   

    print_r($AddDate[0]);闷~被你整晕了!!!连续写错两次..
      

  13.   

    $xml = @file_get_contents("http://aaa.php");
    preg_match_all('/<Simple>(.*)<\/Simple>/i',$xml,$Simple);//得到页面中BuildArea字段所有值
    print_r($Simple[1]);