在PHP中解析XML调查表单

时间:2015-06-25 08:42:33

标签: php xml

我已使用在线转换器将我的Excel调查转换为XForm。请帮助解析PHP中调查的XML脚本,以便我可以访问这些值。 XML文件是:

<?xml version="1.0" encoding="windows-1252" standalone="yes"?><!-- Generated by abcexcel--> 
<Records>
  <Record>
    <Row
      A="type"    
      B="name"
      C="label"
      D="hint"
      E="constratint"
      F="constraint message"
      G="required"
      H="relevant"    
    />
  </Record>
  <Record>
    <Row
      A="select_one male_female"
      B="list_gender"
      C="What is your Gender?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="integer"
      B="age"
      C="What is your Age?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="select_one your_race"
      B="list_race"
      C="What is your Race?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="select_one marital_status"
      B="marital_status"
      C="What is your Marital Status?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="select_multiple health_problems"
      B="health_problems"
      C="What are the three Most Important Community Health Problems?"
      D="(Select all that apply)"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="text"
      B="specify"
      C="Specify Other"
      H="selected (${health_problems},&apos;other&apos;)"
    />
  </Record>
  <Record>
    <Row
      A="select_multiple risk_behaviors"
      B="risky_behaviors"
      C="What are the three Most Important Risky Behaviors?"
      D="(Select all that apply)"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="text"
      B="specify"
      C="Specify Other"
      H="selected (${risky_behaviors},&apos;other&apos;)"
    />
  </Record>
  <Record>
    <Row
      A="select_one commu_health"
      B="comm_health"
      C="How would you rate your Community as a “Healthy Community?”"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="select_one person_health"
      B="pers_health"
      C="How would you Rate your Personal Health?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="select_one edu_level"
      B="edu"
      C="What is your Level of Education?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="select_one income_level"
      B="income"
      C="What is your Household Income?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="select_one pay_treat"
      B="pay_treatment"
      C="How do you Pay for your Healthcare?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="text"
      C="Specify Other"
      H="selected (${pay_treatment},&apos;other&apos;)"
    />
  </Record>
  <Record>
    <Row
      A="select_one involved_govt"
      B="govt_involvement"
      C="Is the Government involved in Improving the Quality of Life?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="text"
      C="If Yes Specify"
      H="${govt_involvement}=&apos;yes&apos;"
    />
  </Record>
  <Record>
    <Row
      A="select_one involved_private"
      B="private_involvement"
      C="Is the Private Sector/ your Employer involved in Improving the Quality of Life?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="text"
      C="If Yes Specify"
      H="${private_involvement}=&apos;yes&apos;"
    />
  </Record>
  <Record>
    <Row
      A="select_one involved_community"
      B="comm-Involvement"
      C="Is your Community involved in Improving the Quality of Life?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="text"
      C="If Yes Specify"
      H="${comm-Involvement}=&apos;yes&apos;"
    />
  </Record>
  <Record>
    <Row
      A="select_one involved_person"
      B="pers_involvement"
      C="Do you take Measures to Improve your Quality of Life?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="text"
      C="If Yes Specify"
      H="${pers_involvement}=&apos;yes&apos;"
    />
  </Record>
  <Record>
    <Row
      A="select_one service_comm"
      B="comm_service"
      C="Approximately how many Hours a Month would you Volunteer your time to Community Service?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="select_multiple info_source"
      B="health_info"
      C="What Source do you often turn to for Reliable Health Information?"
      D="(Select all that apply)"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="text"
      C="Specify Other"
      H="selected (${health_info},&apos;other&apos;)"
    />
  </Record>
  <Record>
    <Row
      A="select_multiple health_centres"
      B="health_facilities"
      C="What are the kinds of Healthcare Facilities available in your Community?"
      D="(select all that apply)"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="text"
      C="Specify Other"
      H="selected (${health_facilities}, &apos;other&apos;)"
    />
  </Record>
  <Record>
    <Row
      A="select_one walk"
      C="How long do you Walk to your Nearest Health Centre?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="select_one visit_clinic"
      B="health_visit"
      C="Do you Always visit the Health Centre when in Need?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="select_multiple reasons_for"
      B="reasons"
      C="If No, which of the following Reasons Hinder you from Visiting the Health Centre?"
      D="(Select all that apply)"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="text"
      C="Specify Other"
      H="selected (${reasons},&apos;other&apos;)"
    />
  </Record>
  <Record>
    <Row
      A="select_one available_equip"
      B="med_equipment"
      C="Does the Health Centre Always have the Supplies/Equipment?"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="select_multiple absent_equip"
      B="absent_equipment"
      C="Which are the Medical Supplies/Equipment that are Commonly Absent?"
      D="(Select all that apply)"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="text"
      C="Specify Other"
      H="selected (${absent_equipment}, &apos;other&apos;)"
    />
  </Record>
  <Record>
    <Row
      A="select_multiple alternative_med"
      B="alt_medicine"
      C="What are some of the Alternative Medical Interventions that you seek rather than Visiting a Health Centre?"
      D="(Select all that apply)"
      G="yes"
    />
  </Record>
  <Record>
    <Row
      A="text"
      C="Specify Other"
      H="selected (${alt_medicine}, &apos;other&apos;)"
    />
  </Record>
  <Record>
    <Row
      A="select_one open_centres"
      B="open"
      C="Are the Healthcare Centres open all the time?"
      G="yes"
    />
  </Record>
</Records>

我在解析时使用以下内容:

<?php
    $survey = new DOMDocument();  //creates the DOM object
    $survey->load("community_health_survey.xml"); //loads the XML file into the created DOM object

    $x = $survey->documentElement; //initialize the XML parser, load the XML and loop through all the elements of the <Records> element
    foreach ($x->childNodes AS $item) {
    print $item->nodeName . " = " . $item->nodeValue . "<br>";
    };

 ?>

1 个答案:

答案 0 :(得分:0)

以下是使用$survey->documentElement->childNodes的两个问题。这是Record个元素节点,但您需要Row个元素。另外$childNodes包括空白文本节点(换行符和缩进)。

您可以使用$survey->getElementsByTagName('Row')。要获取任何Row元素。但更好(更具体)的方法是使用XPath:

$survey = new DOMDocument();
$survey->loadXml($xml);
$xpath = new DOMXPath($survey);

foreach ($xpath->evaluate('/Records/Record/Row') as $row) {
  $data = [];
  foreach ($row->attributes as $attribute) {
    $data[$attribute->name] = $attribute->value;
  }
  var_dump($data);
}

echo $survey->saveXml();

输出:

array(8) {
  ["A"]=>
  string(4) "type"
  ["B"]=>
  string(4) "name"
  ["C"]=>
  string(5) "label"
  ["D"]=>
  string(4) "hint"
  ["E"]=>
  string(11) "constratint"
  ["F"]=>
  string(18) "constraint message"
  ["G"]=>
  string(8) "required"
  ["H"]=>
  string(8) "relevant"
}
array(4) {
  ["A"]=>
  string(22) "select_one male_female"
  ["B"]=>
  string(11) "list_gender"
  ["C"]=>
  string(20) "What is your Gender?"
  ["G"]=>
  string(3) "yes"
}
...

使用XPath,你可以获取第一个&#34; Row&#34;元素(列):

/Records/Record/Row[1]

或者除了第一个之外的所有人:

/Records/Record/Row[position() > 1]