duangenshi9836 2016-04-26 19:38
浏览 80
已采纳

将行/值格式的XML解析为PHP中的自定义对象

Based on what I was reading it seems like the best way to parse XML in PHP is to use the simplexml_load_string() method. Every example that I found consisted of XML that have different child node names. In my case I have XML that is in a row/value format. I want to be able to parse each one of the rows into a custom object called 'Job'. So each row node in the XML represents a 'Job' and each data node represents a Job property. What is the best way to parse this type of XML in PHP?

Here is an example of the XML that I am trying to parse:

<?xml version="1.0" encoding="utf-8"?>
<dataset  xmlns="http://developer.cognos.com/schemas/xmldata/1/"  xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<!--
<dataset
    xmlns="http://developer.cognos.com/schemas/xmldata/1/"
    xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
    xs:schemaLocation="http://developer.cognos.com/schemas/xmldata/1/ xmldata.xsd"
>
-->
    <metadata>
          <item name="RequisitionNumber" type="xs:string" length="52"/>
          <item name="Title" type="xs:string" length="52"/>
          <item name="City" type="xs:string" length="52"/>
          <item name="State" type="xs:string" length="52"/>
          <item name="PostalCode" type="xs:string" length="52"/>
          <item name="Description" type="xs:string" length="52"/>
          <item name="Requirements" type="xs:string" length="52"/>
          <item name="ID" type="xs:string" length="52"/>
    </metadata>
    <data>
        <row>
            <value>16-1279</value>
            <value>Manager</value>
            <value>Portland</value>
            <value>OR</value>
            <value>98660</value>
            <value>Manager Description goes here</value>
            <value>Requirements go here</value>
            <value>45</value>
        </row>
        <row>
            <value>16-1279</value>
            <value>Exec</value>
            <value>Portland</value>
            <value>OR</value>
            <value>98660</value>
            <value>Exec Description goes here</value>
            <value>Exec go here</value>
            <value>45</value>
        </row>
    </data>
</dataset>

This the path I have started down:

class Job
{
        function __construct($requisitionnumber, $title, $city, $state, $postalcode, $description, $requirements)
        {
            $this->RequisitionNumber = $requisitionnumber;
            $this->Title = $title;
            $this->City = $city;
            $this->State = $state;
            $this->PostalCode = $postalcode;
            $this->Description = $description;
            $this->Requirements = $requirements;
        }    
}

function ParseReportResult($xml)
{
    $jobs = array();
    foreach($xml->row) 
    {
        //How do I parse each data node into the below object?
        $job = new Job($requisitionnumber, $title, $city, $state, $postalcode, $description, $requirements);
        array_push($jobs, $job);
    }

    return $jobs;    
}
  • 写回答

2条回答 默认 最新

  • douzhu3367 2016-04-26 20:08
    关注

    Let's say you a variable named $dataset that contains your xml example, you access it as shown below:

    function ParseReportResult($xml)
    {
        $jobs = array();
        foreach($xml->row as $row) 
        {
            $requisitionnumber = (string) $row->value[0];
            $title = (string) $row->value[1];
            $city = (string) $row->value[2];
            $state = (string) $row->value[3];
            $postalcode = (string) $row->value[4];
            $description = (string) $row->value[5];
            $requirements = (string) $row->value[6];
    
            $job = new Job($requisitionnumber, $title, $city, $state, $postalcode, $description, $requirements);
            array_push($jobs, $job);
        }
    
        return $jobs;    
    }
    

    You call your function like this:

    $doc = simplexml_load_string($dataset);
    $jobs = ParseReportResult($doc->data);
    

    You should make sure the values are in the correct order for this to work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?