Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中将xml解析为json,同时在textNode旁边具有子节点_Php_Xml_Json_Simplexml - Fatal编程技术网

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

在php中将xml解析为json,同时在textNode旁边具有子节点,php,xml,json,simplexml,Php,Xml,Json,Simplexml,我需要将一些xml从外部xml-API解析为JSON,为此,我使用了这个非常有用的方法,到目前为止效果非常好。不幸的是,我发现一些文本节点的子节点非常靠近一些简单的文本节点,并且没有作为子节点发现: 简化示例: <?php $str = '<topics> <topic>Objekte mit Data Dictionary Views verwalten <sub_topics> <sub_topic>Data

我需要将一些xml从外部xml-API解析为JSON,为此,我使用了这个非常有用的方法,到目前为止效果非常好。不幸的是,我发现一些文本节点的子节点非常靠近一些简单的文本节点,并且没有作为子节点发现:

简化示例:

<?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
  )
)

SimpleXMLElement对象
(
[主题]=>数组
(
[0]=>Objekte mit数据字典视图verwalten
[1] =>Gröe Datensets bearbeiten
[2] =>verschiedenen Zeitzonen verwalten中的Daten
)
)
当我将xml字符串缩减为仅使用条目时,simplexml\u load\u字符串会发现子节点,但会缩减“标题”:

<?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
                )

        )

)

SimpleXMLElement对象
(
[子主题]=>SimpleXMLElement对象
(
[子主题]=>数组
(
[0]=>数据字典服务器
[1] =>字典视图
[2] =>查看用户对象和所有对象
[3] =>Tabellen-和Spaltenin信息
[4] =>字典查看nach约束信息
[5] =>字典视图包含视图、序列、索引和同义词信息
[6] =>Tabellen Kommentare hinzufügen
[7] =>字典视图nach Kommentarin Formationen abfragen
)
)
)
。。现在我想知道,除了爬到树上,还有什么别的解决办法吗 手动使用xpath的可疑区域,使用数组转换 然后将这些子子子元素合并到数组中

这是我需要解析的完整xml文件的一个示例:

提前谢谢

编辑:
为了解决这个问题,我使用了另一个库,它以更复杂的方式打印数组,并且能够与子元素、属性和节点值分离:

如果您可以通过AJAX请求访问这些数据,我建议您使用jQuery.post()或jQuery.get()函数,它可以像json一样解析XML,另一方面


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

不幸的是,我无法使用ajax获取xml,因为客户端不允许它,因为我的应用程序运行不同的域。为了解决这个问题,我向我的服务器发出一个ajax请求,并使用curl获取xml。然后我可以直接处理数据,并用它做更多的事情(比如将其转换为数组和json以进行进一步编辑)——但这正是我被卡住的地方