dtpfia3334 2011-12-20 09:09
浏览 29
已采纳

在php函数中传递xml并获取具有条件的常见xml [duplicate]

Possible Duplicate:
Best XML Parser for PHP

-----First XML-----str1

<HOME>
    <USER_DETAIL>
        <LOCATION><![CDATA[MUMBAI]]></LOCATION>
        <NAME><![CDATA[RAVI]]></NAME>
        <ID><![CDATA[101]]></ID>
    </USER_DETAIL>
    <USER_DETAIL>
        <LOCATION><![CDATA[PUNE]]></LOCATION>
        <NAME><![CDATA[MANISH]]></NAME>
        <ID><![CDATA[102]]></ID>
    </USER_DETAIL>
</HOME>

---Second XML----str2

<USER_DATA>
    <DATA>
        <DOB><![CDATA[2/11/1959]]></DOB>
        <STATUS><![CDATA[single]]></STATUS>
        <PROFILE_PIC><![CDATA[101.JPG]]></PROFILE_PIC>
        <ID><![CDATA[101]]></ID>
    </DATA>
    <DATA>
        <DOB><![CDATA[6/8/1987]]></DOB>
        <STATUS><![CDATA[married]]></STATUS>
        <PROFILE_PIC><![CDATA[102.JPG]]></PROFILE_PIC>
        <ID><![CDATA[102]]></ID>
    </DATA>
</USER_DATA>

I am new to XML. I have two xml as above I want common xml with all the data of user. In two XML common thing is ID of user. so is there any way to pass xml into function and compare the id in second xml and get the detais of that perticular user and forms a required(Common with all detais) xml.
str1 and str2 is the php variable which contains the above xml respectively.

  • 写回答

1条回答 默认 最新

  • dongwai4434 2011-12-20 11:25
    关注

    use below codes to generate a new XML, you can change the nodes based on your requirements. in case you have both XMLs in a file you can use simplexml_load_file($filename); to load the XML and assign it to $str1 and $str2

    $str1 = "<HOME>
    <USER_DETAIL>
        <LOCATION><![CDATA[MUMBAI]]></LOCATION>
        <NAME><![CDATA[RAVI]]></NAME>
        <ID><![CDATA[101]]></ID>
    </USER_DETAIL>
    <USER_DETAIL>
        <LOCATION><![CDATA[PUNE]]></LOCATION>
        <NAME><![CDATA[MANISH]]></NAME>
        <ID><![CDATA[102]]></ID>
    </USER_DETAIL>
    </HOME>";
    
    $str2 = "<USER_DATA>
    <DATA>
        <DOB><![CDATA[2/11/1959]]></DOB>
        <STATUS><![CDATA[single]]></STATUS>
        <PROFILE_PIC><![CDATA[101.JPG]]></PROFILE_PIC>
        <ID><![CDATA[101]]></ID>
    </DATA>
    <DATA>
        <DOB><![CDATA[6/8/1987]]></DOB>
        <STATUS><![CDATA[married]]></STATUS>
        <PROFILE_PIC><![CDATA[102.JPG]]></PROFILE_PIC>
        <ID><![CDATA[102]]></ID>
    </DATA>
    </USER_DATA>";
    
    $xml1 = simplexml_load_string($str1);
    $xml2 = simplexml_load_string($str1);
    //print_r($xml);
    
    $tempArr = array();
    foreach( $xml1 as $obj)  {
    $id = $obj->ID;
    $tempArr["$id"]['LOCATION'] = (string)$obj->LOCATION;
    $tempArr["$id"]['NAME'] = (string)$obj->NAME;
    }
    foreach( $xml2 as $obj)  {
    $id = $obj->ID; 
    $tempArr["$id"]['DOB'] = (string)$obj->DOB;
    $tempArr["$id"]['STATUS'] = (string)$obj->STATUS;
    $tempArr["$id"]['PROFILE_PIC'] = (string)$obj->PROFILE_PIC;
    }
    
    //print_r($tempArr);
    
    $xml = new DOMDocument('1.0', 'iso-8859-1');
    
    $doc = $xml->createElement('DOCUMENT');
    $doc = $xml->appendChild($doc);
    
    foreach( $tempArr as $ky=>$val )  {
    
    $rnod = $xml->createElement('USER_DETAIL');
    $rnod = $doc->appendChild($rnod);
    //$rnod = $xml->appendChild($rnod);
    
    $dataN0 = $xml->createElement('ID');
    $dataN0 = $rnod->appendChild($dataN0);
    
    $nodV = $xml->createTextNode($ky);
    $dataN0->appendChild($nodV);
    
    foreach($val as $k=>$v)  {
        $dataN1 = $xml->createElement($k);
        $dataN1 = $rnod->appendChild($dataN1);
    
        $nodV1 = $xml->createTextNode($v);
        $dataN1->appendChild($nodV1);
    }
    }
    
    $newXml = $xml->saveXML();
    echo htmlspecialchars($newXml);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输