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 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)