试图在PHP中读取XML文件变量

时间:2013-03-30 11:02:31

标签: php parsing xml-parsing

我有一个像这样的xml计划列表:

<plans>
  <plan>
        <name>LITE</name>
        <description>description</description>
        <rate>$9/Month</rate>
        <price>5</price>
        <period>1</period>
        <units>MONTH</units>
        <recurring>1</recurring>
        <acl>2</acl>
    </plan>
    <plan>
        <name>PRO</name>
        <description>description</description>
        <rate>$10/Year</rate>
        <price>5</price>
        <period>1</period>
        <units>YEAR</units>
        <recurring>0</recurring>
        <acl>3</acl>
    </plan>
</plans>

但我不确定如何解析“期间”和“单位”参数。我这样想但是我觉得有些不对劲 - 比如我没有正确地解决这个问题。

    //$plan is already set above.   
    $plans_xml = simplexml_load_file( "../plans.xml" );
    foreach ($plans_xml->plan->name as $item){
        //if the plan in the xml matches the plan the user signed up for
        if ($item == $plan){
        //set interval by concatenating period and units
        $interval = ($plans_xml->plan->period . $plans_xml->plan->units);
        }
    }

1 个答案:

答案 0 :(得分:0)

foreach ($plans_xml->plan as $item){
    var_dump( $item->name );
}

给出, LITE PRO

那么你可以进行有条件的检查