在 PHP 中将 XML 解析为 json,同时在 textNode 旁边具有子节点


parse xml to json in php while having subnodes next to a textNode

我需要将一些XML从外部XML-API解析为JSON,为此,我使用了IBM的这个非常好的小库,到目前为止效果很好。不幸的是,我发现一些文本节点的子节点紧挨着一些简单的文本节点,并且没有被发现为子节点:

简化示例:

<?php
$str = 
'<topics>
  <topic>Objekte mit Data Dictionary Views verwalten
    <sub_topics>
      <sub_topic>Data Dictionary erläutern</sub_topic>
      <sub_topic>Dictionary Views</sub_topic>
      <sub_topic>Views USER_OBJECTS und ALL_OBJECTS</sub_topic>
      <sub_topic>Tabellen- und Spalteninformationen</sub_topic>
      <sub_topic>Dictionary Views nach Constraint-Informationen abfragen</sub_topic>
      <sub_topic>Dictionary Views nach View-, Sequence-, Index- und Synonyminformationen abfragen</sub_topic>
      <sub_topic>Tabellen Kommentare hinzufügen</sub_topic>
      <sub_topic>Dictionary Views nach Kommentarinformationen abfragen</sub_topic>
    </sub_topics>
  </topic>
  <topic>Große Datensets bearbeiten
    <sub_topics>
      <sub_topic>Daten mithilfe von Unterabfragen bearbeiten</sub_topic>
      <sub_topic>Daten mit einer Unterabfrage als Quelle abrufen</sub_topic>
      <sub_topic>INSERT-Anweisungen mit einer Unterabfrage als Ziel</sub_topic>
      <sub_topic>Schlüsselwort WITH CHECK OPTION in DML-Anweisungen</sub_topic>
      <sub_topic>Anweisung INSERT für mehrere Tabellen – Varianten</sub_topic>
      <sub_topic>Anweisung INSERT für mehrere Tabellen</sub_topic>
      <sub_topic>Zeilen in einer Tabelle zusammenführen</sub_topic>
      <sub_topic>Über einen Zeitraum erfolgte Datenänderungen überwachen</sub_topic>
    </sub_topics>
  </topic>
  <topic>Daten in verschiedenen Zeitzonen verwalten
    <sub_topics>
      <sub_topic>Zeitzonen</sub_topic>
      <sub_topic>CURRENT_DATE, CURRENT_TIMESTAMP und LOCALTIMESTAMP</sub_topic>
      <sub_topic>Datum und Uhrzeit in einer Sessionzeitzone vergleichen</sub_topic>
      <sub_topic>DBTIMEZONE und SESSIONTIMEZONE</sub_topic>
      <sub_topic>DATE und TIMESTAMP – Unterschiede</sub_topic>
      <sub_topic>Datentypen INTERVAL</sub_topic>
      <sub_topic>EXTRACT, TZ_OFFSET und FROM_TZ</sub_topic>
      <sub_topic>TO_TIMESTAMP, TO_YMINTERVAL und TO_DSINTERVAL</sub_topic>
    </sub_topics>
  </topic>
</topics>';
$xml = simplexml_load_string($str, 'SimpleXMLElement', LIBXML_XINCLUDE);
print_r($xml);
?>
SimpleXMLElement Object
(
  [topic] => Array
  (
    [0] => Objekte mit Data Dictionary Views verwalten
    [1] => Größe Datensets bearbeiten
    [2] => Daten in verschiedenen Zeitzonen verwalten
  )
)

当我将 xml 字符串减少为仅使用该条目时,simplexml_load_string发现子节点 - 但减少了"标题":

<?php
$str = '<topic>Objekte mit Data Dictionary Views verwalten
      <sub_topics>
        <sub_topic>Data Dictionary erläutern</sub_topic>
        <sub_topic>Dictionary Views</sub_topic>
        <sub_topic>Views USER_OBJECTS und ALL_OBJECTS</sub_topic>
        <sub_topic>Tabellen- und Spalteninformationen</sub_topic>
        <sub_topic>Dictionary Views nach Constraint-Informationen abfragen</sub_topic>
        <sub_topic>Dictionary Views nach View-, Sequence-, Index- und Synonyminformationen abfragen</sub_topic>
        <sub_topic>Tabellen Kommentare hinzufügen</sub_topic>
        <sub_topic>Dictionary Views nach Kommentarinformationen abfragen</sub_topic>
      </sub_topics>
    </topic>';
$xml = simplexml_load_string($str);
print_r($xml);
?>
SimpleXMLElement Object
(
    [sub_topics] => SimpleXMLElement Object
        (
            [sub_topic] => Array
                (
                    [0] => Data Dictionary erläutern
                    [1] => Dictionary Views
                    [2] => Views USER_OBJECTS und ALL_OBJECTS
                    [3] => Tabellen- und Spalteninformationen
                    [4] => Dictionary Views nach Constraint-Informationen abfragen
                    [5] => Dictionary Views nach View-, Sequence-, Index- und Synonyminformationen abfragen
                    [6] => Tabellen Kommentare hinzufügen
                    [7] => Dictionary Views nach Kommentarinformationen abfragen
                )
        )
)

..现在我想知道除了爬进去之外是否有其他解决方案使用 XPath 手动处理可疑区域,将数组转换为这些子子项并在之后合并数组。

这是我需要解析的完整 xml 文件的一个例子:http://education.oracle.com/pls/web_prod-plq-dad/catalogs.xml_desc?p_id=D49988DE20&p_org_id=34&p_lang=D

提前致谢

编辑:为了解决这个问题,我使用了一个不同的库,它以更复杂的方式打印出数组,并且能够与子元素、属性和节点值分离:http://www.criticaldevelopment.net/xml/doc.php

如果你可以通过AJAX请求访问这些数据,我建议你使用 jQuery.post()或jQuery.get()函数,它可以像json一样解析XML,反之亦然。

它是javascript,但它绝对是最简单的方法(据我所知),无需花费数小时进行编码和搜索即可进行治疗,也许不是最适合您需求的解决方案,但这是您应该寻找的一种方式。