php—是否可以在一维数组中获取所有xml数据?

zqdjd7g9  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(238)

例子:

<school>
  <students>
    <student idNumber="123" enrollmentNo="E101">
      <name>
        <firstname>Jack</firstname>
        <lastname>Harmor</lastname>
        <standard>7th</standard>
      </name>
    </student>
    <student idNumber="243">
      <name>
        <firstname>Cris</firstname>
        <lastname>Warner</lastname>
        <standard>5th</standard>
      </name>
    </student>
    <student idNumber="345">
      <name>
        <firstname>Rick</firstname>
        <lastname>Corner</lastname>
        <standard>6th</standard>
      </name>
    </student>
  </students>
  <fees type="teaching">
    <fifth>100</fifth>
    <sixth>110</sixth>
    <seventh>120</seventh>
  </fees>
</school>

我想创建一个如下所示的数组

array(
  [students_student_name_firstname0]=>Jack,
  [students_student_name_lastname0]=>Harmor,
  [students_student_name_standard0]=>7th,
  [students_student_name_firstname1]=>Cris,
  [students_student_name_lastname1]=>Warner,
  [students_student_name_standard1]=>5th,
  [students_student_name_firstname2]=>Rick,
  [students_student_name_lastname2]=>Corner,
  [students_student_name_standard2]=>6th,
  [students_fifth0]=>100,
  [students_sixth0]=>110,
  [students_seventh0]=>120,
  [attributes]=>array(
    [student0]=>array(
      [idNumber]=>123,
      [enrollmentNo]=>E101
    ),
    [student1]=>array(
      [idNumber]=>243
    ),
    [student2]=>array(
      [idNumber]=>345
    )
  )
);

上面是一个例子,这种xml可以被发送来获取数据,但问题是有任何级别的xml可以是级别1,2,3,。。,n那么我怎样才能像上面那样获得一维/二维数组中的所有数据(它意味着数组的有限级别)以及动态存储其键名和值、属性名和特定标记的值的最简单方法是什么?或者,如果你有更好的方法,那么你也可以分享。我对此一无所知,所以请帮帮我。提前谢谢。

yxyvkwin

yxyvkwin1#

对于直接转换为数组,您可以在循环中处理它,只需执行以下操作:

$array = json_decode(json_encode(simplexml_load_string($xml)), true);

但是,如果你想尽可能的变平,你可以做一些额外的事情,比如:

$xml = '
<school>
  <students>
    <student idNumber="123" enrollmentNo="E101">
      <name>
        <firstname>Jack</firstname>
        <lastname>Harmor</lastname>
        <standard>7th</standard>
      </name>
    </student>
    <student idNumber="243">
      <name>
        <firstname>Cris</firstname>
        <lastname>Warner</lastname>
        <standard>5th</standard>
      </name>
    </student>
    <student idNumber="345">
      <name>
        <firstname>Rick</firstname>
        <lastname>Corner</lastname>
        <standard>6th</standard>
      </name>
    </student>
  </students>
  <fees type="teaching">
    <fifth>100</fifth>
    <sixth>110</sixth>
    <seventh>120</seventh>
  </fees>
</school>
';

$array = json_decode(json_encode(simplexml_load_string($xml)), true);

$result = [];
foreach ($array['students']['student'] as $element) {
    $data = [];

    foreach ($element as $elements) {
        foreach ($elements as $k => $v) {
            $data[$k] = $v;
        }
    }

    $result[] = $data;
}

$fees = [];
foreach ($array['fees'] as $k => $v) {
    if ($k == '@attributes') {
        foreach ($v as $attr => $val) {
            $fees[$attr] = $val;
        }
    } else {
        $fees[$k] = $v;
    }
}

$result['fees'] = $fees;

echo '<pre>';
print_r($result);

结果:

Array
(
    [0] => Array
        (
            [idNumber] => 123
            [enrollmentNo] => E101
            [firstname] => Jack
            [lastname] => Harmor
            [standard] => 7th
        )

    [1] => Array
        (
            [idNumber] => 243
            [firstname] => Cris
            [lastname] => Warner
            [standard] => 5th
        )

    [2] => Array
        (
            [idNumber] => 345
            [firstname] => Rick
            [lastname] => Corner
            [standard] => 6th
        )

    [fees] => Array
        (
            [type] => teaching
            [fifth] => 100
            [sixth] => 110
            [seventh] => 120
        )

)
csga3l58

csga3l582#

为了能够获得中有值的所有文本节点(即排除任何文档中的正常空白)和所有属性,可以使用以下代码。请注意,这并没有为您提供与您提供的格式相同的格式,但该格式应允许您在之后提出一些内容。。

$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->load($file);
$xp = new DOMXPath($xml);
$entries = $xp->query("//node()[not(node())][string-length(normalize-space(.) ) > 1]");
foreach ( $entries as $data )   {
    echo $data->getNodePath()."=".$data->nodeValue.PHP_EOL;
}

echo "Attributes".PHP_EOL;
$attributes = $xp->query("//@*");
foreach ( $attributes as $data )   {
    echo $data->getNodePath()."=".$data->nodeValue.PHP_EOL;
}

给予。。。

/school/students/student[1]/name/firstname/text()=Jack
/school/students/student[1]/name/lastname/text()=Harmor
/school/students/student[1]/name/standard/text()=7th
/school/students/student[2]/name/firstname/text()=Cris
/school/students/student[2]/name/lastname/text()=Warner
/school/students/student[2]/name/standard/text()=5th
/school/students/student[3]/name/firstname/text()=Rick
/school/students/student[3]/name/lastname/text()=Corner
/school/students/student[3]/name/standard/text()=6th
/school/fees/fifth/text()=100
/school/fees/sixth/text()=110
/school/fees/seventh/text()=120
Attributes
/school/students/student[1]/@idNumber=123
/school/students/student[1]/@enrollmentNo=E101
/school/students/student[2]/@idNumber=243
/school/students/student[3]/@idNumber=345
/school/fees/@type=teaching

您可能需要删掉名称的某些部分(即。 text() )等等。
尽管坚持按原样处理xml要好得多。

相关问题