0

我需要将一些 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

4

1 回答 1

0

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

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

于 2012-06-12T15:23:45.900 回答