如何将大型json文件转换为xml

时间:2022-01-21 06:52:20

I have very big .json file (over 5GB) and I want to convert it to .xml format. Is there any software or way (it can be everything) to do that?

我有非常大的.json文件(超过5GB),我想将其转换为.xml格式。是否有任何软件或方式(可以是一切)?

I found XML Editor

我找到了XML编辑器

but there is just xml => json converter.

但只有xml => json转换器。

2 个解决方案

#1


1  

The typical way to process XML as well as JSON files is to load these files completely into memory. Then you have a so called DOM which allows you various kinds of data processing. But neither XML nor JSON are really designed for storing that much data you have here. To my experience you typically will run into memory problems as soon as you exceed a 200 MByte limit. This is because DOMs are created that are composed from individual objects. This approach results in a huge memory overhead that far exceeds the amount of data you want to process.

处理XML和JSON文件的典型方法是将这些文件完全加载到内存中。然后你有一个所谓的DOM,它允许你进行各种数据处理。但是XML和JSON都不是真的用于存储你在这里拥有的那么多数据。根据我的经验,一旦超过200 MB的限制,通常会遇到内存问题。这是因为创建了由单个对象组成的DOM。这种方法会导致巨大的内存开销,远远超过您要处理的数据量。

The only way for you to process files like that is basically to take a stream approach. The basic idea: Instead of parsing the whole file and loading it into memory you parse and process the file "on the fly". As data is read it is parsed and events are triggered on which your software can react and perform some actions as needed. (For details on that have a look at the SAX API in order to understand this concept in more detail.)

处理这样的文件的唯一方法基本上是采用流方法。基本思想:不是解析整个文件并将其加载到内存中,而是“动态”解析和处理文件。在读取数据时,它会被解析并触发事件,您的软件可以根据需要做出反应并执行某些操作。 (有关详细信息,请查看SAX API以更详细地了解此概念。)

As you stated you are processing JSON, not XML. Stream APIs for JSON should be available in the wild as wel. Anyway you could implement one fairly easily yourself: JSON is a pretty simple data format.

正如您所说,您正在处理JSON,而不是XML。 JSON的流API也应该可以在野外使用。无论如何,你可以自己轻松地实现一个:JSON是一种非常简单的数据格式。

Nevertheless such an approach is not optimal: Typically such a concept will result in very slow data processing because of millions of method invocations involved: For every item encountered you typically need to call a method in order to perform some data processing task. This together with additional checks about what kind of information you currently have encountered in the stream will slow down data processing pretty much.

然而,这种方法并不是最优的:通常这样的概念会导致数据处理速度非常慢,因为涉及到数百万个方法调用:对于遇到的每个项目,您通常需要调用方法来执行某些数据处理任务。这与对当前在流中遇到的信息类型的额外检查一起将大大减慢数据处理速度。

You really should consider to use a different kind of approach. First split your file into many small ones, then perform processing on them. This approach might not seem to be very elegant, but it helps to keep your task much simpler. This way you gain a main advantage: It will be much easier for you to debug your software.

你真的应该考虑使用不同的方法。首先将文件拆分成许多小文件,然后对它们执行处理。这种方法似乎不是很优雅,但它有助于使您的任务更简单。这样您就获得了一个主要优势:您可以更轻松地调试软件。

#2


1  

If you're willing to use the XML Serializer from PEAR, you can convert the JSON to a PHP object and then the PHP object to XML in two easy steps:

如果您愿意使用PEAR中的XML Serializer,您可以通过两个简单的步骤将JSON转换为PHP对象,然后将PHP对象转换为XML:

check this link for more

查看此链接了解更多信息

convert json to xml

将json转换为xml

a little example

一个小例子

include("XML/Serializer.php");

function json_to_xml($json) {
    $serializer = new XML_Serializer();
    $obj = json_decode($json);

    if ($serializer->serialize($obj)) {
        return $serializer->getSerializedData();
    }
    else {
        return null;
    }
}

good luck and try

祝你好运并尝试

#1


1  

The typical way to process XML as well as JSON files is to load these files completely into memory. Then you have a so called DOM which allows you various kinds of data processing. But neither XML nor JSON are really designed for storing that much data you have here. To my experience you typically will run into memory problems as soon as you exceed a 200 MByte limit. This is because DOMs are created that are composed from individual objects. This approach results in a huge memory overhead that far exceeds the amount of data you want to process.

处理XML和JSON文件的典型方法是将这些文件完全加载到内存中。然后你有一个所谓的DOM,它允许你进行各种数据处理。但是XML和JSON都不是真的用于存储你在这里拥有的那么多数据。根据我的经验,一旦超过200 MB的限制,通常会遇到内存问题。这是因为创建了由单个对象组成的DOM。这种方法会导致巨大的内存开销,远远超过您要处理的数据量。

The only way for you to process files like that is basically to take a stream approach. The basic idea: Instead of parsing the whole file and loading it into memory you parse and process the file "on the fly". As data is read it is parsed and events are triggered on which your software can react and perform some actions as needed. (For details on that have a look at the SAX API in order to understand this concept in more detail.)

处理这样的文件的唯一方法基本上是采用流方法。基本思想:不是解析整个文件并将其加载到内存中,而是“动态”解析和处理文件。在读取数据时,它会被解析并触发事件,您的软件可以根据需要做出反应并执行某些操作。 (有关详细信息,请查看SAX API以更详细地了解此概念。)

As you stated you are processing JSON, not XML. Stream APIs for JSON should be available in the wild as wel. Anyway you could implement one fairly easily yourself: JSON is a pretty simple data format.

正如您所说,您正在处理JSON,而不是XML。 JSON的流API也应该可以在野外使用。无论如何,你可以自己轻松地实现一个:JSON是一种非常简单的数据格式。

Nevertheless such an approach is not optimal: Typically such a concept will result in very slow data processing because of millions of method invocations involved: For every item encountered you typically need to call a method in order to perform some data processing task. This together with additional checks about what kind of information you currently have encountered in the stream will slow down data processing pretty much.

然而,这种方法并不是最优的:通常这样的概念会导致数据处理速度非常慢,因为涉及到数百万个方法调用:对于遇到的每个项目,您通常需要调用方法来执行某些数据处理任务。这与对当前在流中遇到的信息类型的额外检查一起将大大减慢数据处理速度。

You really should consider to use a different kind of approach. First split your file into many small ones, then perform processing on them. This approach might not seem to be very elegant, but it helps to keep your task much simpler. This way you gain a main advantage: It will be much easier for you to debug your software.

你真的应该考虑使用不同的方法。首先将文件拆分成许多小文件,然后对它们执行处理。这种方法似乎不是很优雅,但它有助于使您的任务更简单。这样您就获得了一个主要优势:您可以更轻松地调试软件。

#2


1  

If you're willing to use the XML Serializer from PEAR, you can convert the JSON to a PHP object and then the PHP object to XML in two easy steps:

如果您愿意使用PEAR中的XML Serializer,您可以通过两个简单的步骤将JSON转换为PHP对象,然后将PHP对象转换为XML:

check this link for more

查看此链接了解更多信息

convert json to xml

将json转换为xml

a little example

一个小例子

include("XML/Serializer.php");

function json_to_xml($json) {
    $serializer = new XML_Serializer();
    $obj = json_decode($json);

    if ($serializer->serialize($obj)) {
        return $serializer->getSerializedData();
    }
    else {
        return null;
    }
}

good luck and try

祝你好运并尝试