使用内联HTML元素在PHP中将XML转换为JSON

时间:2019-05-17 00:28:55

标签: php json simplexml

我正在尝试使用simplexml在PHP中将一些XML转换为JSON,但是这种转换没有内联HTML。我不确定内联HTML(嵌入?)的正确术语,但是下面是我正在做什么和正在寻找什么的一些示例:

我要转换的XML摘录:

<uscDoc xsi:schemaLocation="http://xml.house.gov/schemas/uslm/1.0 USLM-1.0.15.xsd" xml:lang="en" identifier="/us/usc/t1" xmlns="http://xml.house.gov/schemas/uslm/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/">
    <meta>
      <dc:title>Title 1</dc:title>
      <dc:type>USCTitle</dc:type>
      <docNumber>1</docNumber>
      <docPublicationName>Online@116-17</docPublicationName>
      <dc:publisher>OLRC</dc:publisher>
      <dcterms:created>2019-04-04T07:37:52</dcterms:created>
      <dc:creator>USCConverter 1.1</dc:creator>
    </meta>
    <main>
    <title id="id14e8bf81-56ce-11e9-9468-8af4f27dcc3b" identifier="/us/usc/t1"><num value="1">Title 1—</num><heading>GENERAL PROVISIONS</heading>
    <note topic="enacting" id="id14e90da2-56ce-11e9-9468-8af4f27dcc3b"><p>This title was enacted by <ref href="/us/act/1947-07-30/ch388">act July 30, 1947, ch. 388</ref>, § 1, <ref href="/us/stat/61/633">61 Stat. 633</ref></p></note>
    <note topic="miscellaneous" id="id14e95bc3-56ce-11e9-9468-8af4f27dcc3b"><p>Current through 116-17</p></note>

当我在PHP中将其转换为JSON(或数组)时,结果如下:

Array
(
    [@attributes] => Array
        (
            [identifier] => /us/usc/t1
        )

    [meta] => Array
        (
            [docNumber] => 1
            [docPublicationName] => Online@116-17
        )

    [main] => Array
        (
            [title] => Array
                (
                    [@attributes] => Array
                        (
                            [id] => id14e8bf81-56ce-11e9-9468-8af4f27dcc3b
                            [identifier] => /us/usc/t1
                        )

                    [num] => Title 1—
                    [heading] => GENERAL PROVISIONS
                    [note] => Array
                        (
                            [0] => Array
                                (
                                    [@attributes] => Array
                                        (
                                            [topic] => enacting
                                            [id] => id14e90da2-56ce-11e9-9468-8af4f27dcc3b
                                        )

                                    [p] => This title was enacted by , § 1,
                                )

                            [1] => Array
                                (
                                    [@attributes] => Array
                                        (
                                            [topic] => miscellaneous
                                            [id] => id14e95bc3-56ce-11e9-9468-8af4f27dcc3b
                                        )

                                    [p] => Current through 116-17
                                )

                        )

不正确的部分(以及其他遗漏的数据)是包含以下内容的部分:“此标题由§1制定,”

当我使用在线JavaScript XML将该XML转换为JSON转换器(http://www.utilities-online.info/xmltojson/)时,它会正确转换:

{
  "uscDoc": {
    "-xsi:schemaLocation": "http://xml.house.gov/schemas/uslm/1.0 USLM-1.0.15.xsd",
    "-xml:lang": "en",
    "-identifier": "/us/usc/t1",
    "-xmlns": "http://xml.house.gov/schemas/uslm/1.0",
    "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "-xmlns:dc": "http://purl.org/dc/elements/1.1/",
    "-xmlns:dcterms": "http://purl.org/dc/terms/",
    "meta": {
      "dc:title": "Title 1",
      "dc:type": "USCTitle",
      "docNumber": "1",
      "docPublicationName": "Online@116-17",
      "dc:publisher": "OLRC",
      "dcterms:created": "2019-04-04T07:37:52",
      "dc:creator": "USCConverter 1.1"
    },
    "main": {
      "title": {
        "-id": "id14e8bf81-56ce-11e9-9468-8af4f27dcc3b",
        "-identifier": "/us/usc/t1",
        "num": {
          "-value": "1",
          "#text": "Title 1—"
        },
        "heading": "GENERAL PROVISIONS",
        "note": [
          {
            "-topic": "enacting",
            "-id": "id14e90da2-56ce-11e9-9468-8af4f27dcc3b",
            "p": {
              "#text": [
                "This title was enacted by ",
                ", § 1, "
              ],
              "ref": [
                {
                  "-href": "/us/act/1947-07-30/ch388",
                  "#text": "act July 30, 1947, ch. 388"
                },
                {
                  "-href": "/us/stat/61/633",
                  "#text": "61 Stat. 633"
                }
              ]
            }
          },

如您所见,“此标题是由...制定的”行已正确分解,因此可以散布在其下的数据中,包括“ 1947年7月30日生效,第388章”和“ 61条规定633” 。”

我已经尝试过在simplexml中找到的每个选项,以使其像联机XML到JSON解析器那样解析XML,但是我无法使其工作。我在这里和其他地方都尝试了无数其他PHP代码段,但无法获得相同的结果。

任何帮助将不胜感激。

0 个答案:

没有答案