请问PHP 一般用什么来解析XML  我有一个比较复杂的XML需要解析 用DOMDocument 可以么

解决方案 »

  1. leizhpucn:
    php有simpleXML类,简单、方便;
      

  2. php来解析xml文档,手册上有,可以用xml_parse_into_struct()函数尝试一下。
      

  3. $XMLstr= < < <STR
    <?xml version="1.0"?>
    <DMResponse>
        <Code>1 </Code>
        <Description>Your request was successfully received and processed. </Description>
        <ResultData>
            <Explanation> Here is the response to your request </Explanation>
            <GetObjectNamesRs>
            <ClientTree>
              <ClientName>Client_Week </ClientName>
              <SiteTree>
                    <SiteName>SiteMon </SiteName>
                    <CampaignTree>
                        <CampaignName>Cam_Mon_Child_0 </CampaignName>
                        <MailingName>Mon__Mailing </MailingName>
                        <MailingType>StandardMailing </MailingType>
                        <MailingName>Mon__Mailing_1 </MailingName>
                        <MailingType>StandardMailing </MailingType>
                        <MailingName>Mon__Mailing_2 </MailingName>
                        <MailingType>StandardMailing </MailingType>
                    </CampaignTree>
              </SiteTree>
              <SiteTree>
                <SiteName>Site_Week </SiteName>
                <CampaignTree>
                    <CampaignName>Cam_Week </CampaignName>
                </CampaignTree>
                <CampaignTree>
                    <CampaignName>Cam_Week_Child_0 </CampaignName>
                    <MailingName>Weekly__Mailing </MailingName>
                    <MailingType>StandardMailing </MailingType>
                    <MailingName>Weekly__Mailing_1 </MailingName>
                    <MailingType>StandardMailing </MailingType>
                    <MailingName>Weekly__Mailing_2 </MailingName>
                    <MailingType>StandardMailing </MailingType>
                </CampaignTree>
                <CampaignTree>
                    <CampaignName>WeeklyCampaign_2009_52 </CampaignName>
                </CampaignTree>
                <CampaignTree>
                    <CampaignName>C_20080209 </CampaignName>
                    <MailingName>investing_20080209_Other-Copy </MailingName>
                    <MailingType>StandardMailing </MailingType>
                    <MailingName>investing_20080209_Global-Copy </MailingName>
                    <MailingType>StandardMailing </MailingType>
                    <MailingName>investing_20080209_Other </MailingName>
                    <MailingType>StandardMailing </MailingType>
                    <MailingName>investing_20080209_Netease </MailingName>
                    <MailingType>StandardMailing </MailingType>
                    <MailingName>investing_20080209_Global </MailingName>
                    <MailingType>StandardMailing </MailingType>
                </CampaignTree>
                <CampaignTree>
                    <CampaignName>C_20080203 </CampaignName>
                    <MailingName>investing_20080203_Netease </MailingName>
                    <MailingType>StandardMailing </MailingType>
                    <MailingName>investing_20080203_Global </MailingName>
                    <MailingType>StandardMailing </MailingType>
                </CampaignTree>
                <CampaignTree>
                    <CampaignName>C_20071026 </CampaignName>
                    <MailingName>news_20071026_Other </MailingName>
                    <MailingType>StandardMailing </MailingType>
                    <MailingName>news_20071026_Netease </MailingName>
                    <MailingType>StandardMailing </MailingType>
                    <MailingName>news_20071026_Global </MailingName>
                    <MailingType>StandardMailing </MailingType>
                </CampaignTree>
                </SiteTree>
            </ClientTree>
            </GetObjectNamesRs>
        </ResultData>
    </DMResponse>
    STR;
    $dom = new DOMDocument();
    $dom->loadXML($XMLstr);$dompath = new DOMXPath($dom);
    $nodes = $dompath->query('//ResultData/GetObjectNamesRs/ClientTree/SiteTree/SiteName');$sitename = array();foreach ($nodes as $node)
    {
        $sitename[] = $node->nodeValue;
    }print_r($sitename);
      

类似问题 »