我需要在Parse.com的Cloud Code上的JavaScript中将XML转换为Json [英] I need to convert XML to Json in JavaScript on Parse.com's Cloud Code

查看:75
本文介绍了我需要在Parse.com的Cloud Code上的JavaScript中将XML转换为Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要说您不能使用REGEX解析XML,这可以做到,这不是最好的方法.并且请不要否定这个问题.

PLEASE DO NOT COMMENT SAYING YOU CANNOT PARSE XML WITH REGEX, IT CAN BE DONE IT'S JUST NOT THE BEST WAY. AND PLEASE DON'T NEGATIVE THIS QUESTION FOR NO REASON.

在Parse.com的云代码上,如果没有大量的编码工作,您当前无法将XML转换为json.我在以下位置找到了以下代码: http://killzonekid.com /worlds-smallest-fastest-xml-to-json-javascript-converter/

On Parse.com's cloud code you currently cannot convert XML to json without major coding effort. I found the following code at: http://killzonekid.com/worlds-smallest-fastest-xml-to-json-javascript-converter/

xml = xml.replace(/\s/g, ' ').replace(/< *\?[^>]*?\? *>/g, '').replace(/< *!--[^>]*?-- *>/g, '').replace(/< *(\/?) *(\w+\b):(\w+\b)/g, '<$1$2_$3').replace(/< *(\w+\b)([^>]*?)\/ *>/g, '< $1$2>').replace(/(\w+\b):(\w+\b) *= *"([^>]*?)"/g, '$1_$2="$3"').replace(/< *(\w+\b)((?: *\w+ *= *" *[^"]*?")+ *)>( *[^< ]*?\b.*?)< *\/ *\1 *>/g, '< $1$2 value="$3">').replace(/ *(\w+\b) *= *"([^>]*?)" */g, '< $1>$2').replace(/< *(\w+\b) *</g, '<$1>< ').replace(/> *>/g, '>').replace(/< *\/ *(\w+\b) *> *< *\1 *>/g, '').replace(/"/g, '\\"').replace(/< *(\w+\b) *>([^<>]*?)< *\/ *\1 *>/g, '"$1":"$2",').replace(/< *(\w+\b) *>([^<>]*?)< *\/ *\1 *>/g, '"$1":{$2},').replace(/< *(\w+\b) *>(?=.*?< \/\1\},\{)/g, '"$1":[{').split(/\},\{/).reverse().join('},{').replace(/< *\/ *(\w+\b) *>(?=.*?"\1":\[\{)/g, '}],').split(/\},\{/).reverse().join('},{').replace(/< \/(\w+\b)\},\{\1>/g, '},{').replace(/< *(\w+\b)[^>]*?>/g, '"$1":{').replace(/< *\/ *\w+ *>/g,'},').replace(/\} *,(?= *(\}|\]))/g, '}').replace(/] *,(?= *(\}|\]))/g, ']').replace(/" *,(?= *(\}|\]))/g, '"').replace(/ *, *$/g, '');

将XML转换为json确实做得很好.

It actually does quite a good job of converting XML to json.

该代码有一些怪癖. 1.弄乱了属性.

There are a few querks with the code. 1. It messes up the attributes.

  1. 它不喜欢带有连字符的名称. 为了解决连字符问题,我将所有\ w +都更改为\ w [\ w'-],这是最好的方法吗?
  1. It doesn't like names with hyphens in them. To fix the hyphens I changed all the \w+ to \w[\w'-] Is this the best way?

这是XML文档示例

    <?xml version="1.0" encoding="UTF-8" ?>
<api>
    <products total-matched="1618" records-returned="1" page-number="1">
        <product>
            <ad-id>1234</ad-id>
            <supplier-name>Window World</supplier-name>
            <supplier-category>3703703</supplier-category>
            <buy-url>http://website.com</buy-url>
            <currency>USD</currency>
            <description>Window</description>
            <image-url>http://website.com/windowa/80x80.jpg</image-url>
            <in-stock>yes</in-stock>
            <manufacturer-name>Window World</manufacturer-name>
            <name>Half Pain Glass</name>
            <price>31.95</price>
            <retail-price>87.60</retail-price>
            <sale-price>29.95</sale-price>
            <sku>5938</sku>
            <upc></upc>
        </product>
    </products>
</api>

示例输出:

{
    "api": {
        "products": {
            "total-matched": {
                1618 "records-returned": {
                    1 "page-number": {
                        1 >
                            "product": {
                            "adid": "1234",
                            "suppliername": "Window World",
                            "suppliercategory": "3703703",
                            "buyurl": "http://website.com",
                            "currency": "USD",
                            "description": "Window",
                            "imageurl": "http://website.com/windowa/80x80.jpg",
                            "instock": "yes",
                            "manufacturername": "Window World",
                            "name": "Half Pain Glass",
                            "price": "31.95",
                            "retailprice": "87.60",
                            "saleprice": "29.95",
                            "sku": "5938",
                            "upc": ""
                        }
                    }
                }
            }
        }
    }
}

推荐答案

我的猜测是-查看生成的json的结构-应该没有属性.要授权它们,您需要进行一些更改,包括嵌套嵌套的json ...
不能仅仅改变:

My guess is that - looking at the structure of the resulting json - there should be no attributes. To authorize them, you would need to change quite some things, including nesting the nested json...
Ain't it possible to just change:

<products total-matched="1618" records-returned="1" page-number="1">

<products>
  <total-matched>1618</total-matched>
  <records-returned>1</records-returned>
  <page-number>1</page-number>
  <product>...

...因为它将为您带来期望的属性(我想).

...as it would give you what you expect to have with attributes (I guess).

对于连字符,您的想法很好,只需将\ w更改为[\ w-],它应该可以工作(我很乐意承认我没有研究所有的正则表达式,因此再次只是一个猜测) ). \ w +将变为[\ w-] +,依此类推.

As for the hyphens, your idea is good, just change the \w to [\w-], it should work (I'll glady admit I didn't look into all the regexes so it's just a guess once more). \w+ would become [\w-]+ and so on.

您可以添加一个步骤来首先更改xml.此正则表达式应执行以下操作:

You can add a step to first change your xml. This regex should do that part:

/(<\w+[^<]*?)\s+([\w-]+)="([^"]+)">/
// asuming there is no " in your attributes' values (would be more complicated...)

测试:

var string = '<api><products total-matched="1618" records-returned="1" page-number="1">';
var regex = /(<\w+[^<]*?)\s+([\w-]+)="([^"]+)">/;
while(string.match(regex)) string = string.replace(regex, '$1><$2>$3</$2>');

结果:

"<api><products><total-matched>1618</total-matched><records-returned>1</records-returned><page-number>1</page-number>"

这篇关于我需要在Parse.com的Cloud Code上的JavaScript中将XML转换为Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆