首页 > 解决方案 > 如何在php中使用cdata将xml文件转换为json格式?

问题描述

我正在尝试将我的 xml 文件(大文件)转换为 php 中的 json 格式。问题是 json 格式的 cdata 丢失。

这是我的代码,

<?php
$xml = simplexml_load_file('belovedskincare.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
$jsondata = json_encode($xml);
echo $jsondata;
?>

XML 代码:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--  This is a WordPress eXtended RSS file generated by WordPress as an export of your site.  -->
<!--  It contains information about your site's posts, pages, comments, categories, and other content.  -->
<!--  You may use this file to transfer that content from one site to another.  -->
<!--  This file is not intended to serve as a complete backup of your site.  -->
<!--  To import this information into a WordPress site follow these steps:  -->
<!--  1. Log in to that site as an administrator.  -->
<!--  2. Go to Tools: Import in the WordPress admin panel.  -->
<!--  3. Install the "WordPress" importer from the list.  -->
<!--  4. Activate & Run Importer.  -->
<!--  5. Upload this file using the form provided on that page.  -->
<!--  6. You will first be asked to map the authors in this export file to users  -->
<!--     on the site. For each author, you may choose to map to an  -->
<!--     existing user on the site or to create a new user.  -->
<!--  7. WordPress will then import each of the posts, pages, comments, categories, etc.  -->
<!--     contained in this file into your site.  -->
<!--  generator="WordPress/5.6.5" created="2021-10-02 09:17"  -->
<rss xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wp="http://wordpress.org/export/1.2/" version="2.0">
<channel>
<title>Beloved Skincare</title>
<link>https://belovedskincare.com.my</link>
<description>Food for your Skin</description>
<pubDate>Sat, 02 Oct 2021 09:17:04 +0000</pubDate>
<language>en-US</language>
<wp:wxr_version>1.2</wp:wxr_version>
<wp:base_site_url>https://belovedskincare.com.my</wp:base_site_url>
<wp:base_blog_url>https://belovedskincare.com.my</wp:base_blog_url>
<wp:author>
<wp:author_id>1</wp:author_id>
<wp:author_login>
<![CDATA[ admin ]]>
</wp:author_login>
<wp:author_email>
<![CDATA[ udaybtec@gmail.com ]]>
</wp:author_email>
<wp:author_display_name>
<![CDATA[ admin ]]>
</wp:author_display_name>
<wp:author_first_name>
<![CDATA[ Udaya ]]>
</wp:author_first_name>
<wp:author_last_name>
<![CDATA[ Kumar ]]>
</wp:author_last_name>
</wp:author>
<wp:author>
<wp:author_id>113</wp:author_id>
<wp:author_login>
<![CDATA[ studiogulf ]]>
</wp:author_login>
<wp:author_email>
<![CDATA[ uday@studiogulf.com ]]>
</wp:author_email>
<wp:author_display_name>
<![CDATA[ StudioGulf ]]>
</wp:author_display_name>
<wp:author_first_name>
<![CDATA[ Studio ]]>
</wp:author_first_name>
<wp:author_last_name>
<![CDATA[ Gulf ]]>
</wp:author_last_name>
</wp:author>
<generator>https://wordpress.org/?v=5.6.5</generator>
<image>
<url>https://belovedskincare.com.my/wp-content/uploads/2018/06/cropped-Logo-32x32.png</url>
<title>Beloved Skincare</title>
<link>https://belovedskincare.com.my</link>
<width>32</width>
<height>32</height>
</image>
<item>
<title>Coffee & Honey Face Scrub</title>
<link>https://belovedskincare.com.my/product/coffee-honey-face-scrub/</link>
<pubDate>Sun, 31 Dec 2017 17:01:43 +0000</pubDate>
<dc:creator>
<![CDATA[ admin ]]>
</dc:creator>
<guid isPermaLink="false">http://localhost/xstore/product/import-placeholder-for-496/</guid>
<description/>

输出:

"channel": {
"title": "Beloved Skincare",
"link": "https://belovedskincare.com.my",
"description": "Food for your Skin",
"pubDate": "Sat, 02 Oct 2021 09:17:04 +0000",
"language": "en-US",
"generator": "https://wordpress.org/?v=5.6.5",
"image": {
"url": "https://belovedskincare.com.my/wp-content/uploads/2018/06/cropped-Logo-32x32.png",
"title": "Beloved Skincare",
"link": "https://belovedskincare.com.my",
"width": "32",
"height": "32"
},
"item": [
{
"title": "Coffee & Honey Face Scrub",
"link": "https://belovedskincare.com.my/product/coffee-honey-face-scrub/",

但实际输出应该是,

"channel": {
"title": "Beloved Skincare",
"link": "https://belovedskincare.com.my",
"description": "Food for your Skin",
"pubDate": "Sat, 02 Oct 2021 09:17:04 +0000",
"language": "en-US",
"wp:wxr_version": "1.2",
"wp:base_site_url": "https://belovedskincare.com.my",
"wp:base_blog_url": "https://belovedskincare.com.my",
"wp:author": [
{
"wp:author_id": "1",
"wp:author_login": {
"#cdata": "admin"
},
"wp:author_email": {
"#cdata": "udaybtec@gmail.com"
},
"wp:author_display_name": {
"#cdata": "admin"
},
"wp:author_first_name": {
"#cdata": "Udaya"
},
"wp:author_last_name": {
"#cdata": "Kumar"
}
},
{
"wp:author_id": "113",
"wp:author_login": {
"#cdata": "studiogulf"
},
"wp:author_email": {
"#cdata": "uday@studiogulf.com"
},
"wp:author_display_name": {
"#cdata": "StudioGulf"
},
"wp:author_first_name": {
"#cdata": "Studio"
},
"wp:author_last_name": {
"#cdata": "Gulf"
}
}
],
"generator": "https://wordpress.org/?v=5.6.5",
"image": {
"url": "https://belovedskincare.com.my/wp-content/uploads/2018/06/cropped-Logo-32x32.png",
"title": "Beloved Skincare",
"link": "https://belovedskincare.com.my",
"width": "32",
"height": "32"
},
"item": [
{
"title": "Coffee & Honey Face Scrub",
"link": "https://belovedskincare.com.my/product/coffee-honey-face-scrub/",

如您所见,“通道”属性包含的数据非常少。并且通过实际输出,您可以看到它包含更多数据。由于它是一个大文件,我插入了一小部分输出。我希望这么多的输出就足够了。如果您想了解更多详细信息,请在评论中提及。如果可能的话,我会插入它。

标签: phpjsonxmlcdataxml-to-json

解决方案


推荐阅读