【问题标题】:js parsing XML from JSP, storing valuesjs从JSP解析XML,存储值
【发布时间】:2011-06-08 18:57:13
【问题描述】:

我正在解析 XML,并希望以易于阅读的方式存储这些值。

我得到了 mapShapes 和 mapPoints(所以 x 和 y 坐标)。 (x,y) 对构成一个点,点的集合构成一个形状。

这是我想解析的一些示例 XML:

<?xml version='1.0'?>
<mapQuery id='10918014'>
    <mapShape id='429436'>
        <mapPoint id='4259799'>
            <x>-81.61508</x>
            <y>41.52184</y>
        </mapPoint>
        <mapPoint id='4259800'>
            <x>-81.61537</x>
            <y>41.52181</y>
        </mapPoint>
        <mapPoint id='4259801'>
            <x>-81.61538</x>
            <y>41.522198</y>
        </mapPoint>
        <mapPoint id='4259802'>
            <x>-81.61516</x>
            <y>41.52222</y>
        </mapPoint>
        <mapPoint id='4259803'>
            <x>-81.61508</x>
            <y>41.52184</y>
        </mapPoint>
    </mapShape>
</mapQuery>

我想得到一个类似的数组

shapes[0].point[0].x[0] = first x point
shapes[0].point[0].y[0] = first y point

(此示例 XML 仅存在一个形状,但可能有多个。)

提前感谢您对一个简单问题的帮助 =)

这是一些骨架代码:

shapeXmlHandler : function(xml){

    $(xml).find("mapShape").each(function(){
        $(this).find("mapPoint").each(function() {
            console.log('mapPoint: '+$(this).attr('id'));
            console.log('x :'+$(this).find("x").text());
            console.log('y :'+$(this).find("y").text());
        });
    });
}

【问题讨论】:

  • 您遇到了什么问题?到目前为止看起来不错(除了我认为您实际上想要shapes[0].point[0].x = first x point 等)
  • 好电话。我不确定如何实现目标,但我已经很接近了。我真的不知道在解析它时将 xml 数据放入数组中的最佳方法是什么,所以我在寻求建议

标签: javascript jquery xml arrays


【解决方案1】:

尝试使用Array.push()

shapeXmlHandler : function(xml)
{
    var shapes = [];

    $(xml).find('mapShape').each(function()
    {
        var shape = [];

        $(this).find('mapPoint').each(function()
        {
            var $p = $(this),
                point = 
                {
                    x: $p.find('x').text(),
                    y: $p.find('y').text()
                };

            shape.push(point);
        });

        shapes.push(shape);
    });

    console.log(shapes);
}

这应该记录类似

[
    [
        {x: -81.61508, y: 41.52184},
        {x: -81.61537, y: 41.52181},
        ...
    ]
]

这可以使用.map() 而不是.each() 来完成一点点:

shapeXmlHandler : function(xml)
{
    var shapes = $(xml).find('mapShape').map(function()
    {
        return $(this).find('mapPoint').map(function()
        {
            var $p = $(this);
            return {
                x: $p.find('x').text(),
                y: $p.find('y').text()
            };
        }).get();
    }).get();

    console.log(shapes);
}

如果有任何机会使用 JSON 而不是 XML,那么您根本不需要任何自定义解析代码!只需使用$.parseJSON

【讨论】:

  • 非常感谢您的详尽回答。如果可以的话,会给你两个赞成票=)
  • 谢谢,不用担心。您最终使用了哪种方法?
  • .map() 需要 .get() 吗?除非我关闭 .get() ,否则我不会得到任何控制台输出,但它显示的内容不仅仅是数组(如各种 jQuery 方法)。我可能正在使用第一种方法 - 仍然不确定我是否需要长时间保持输入,所以我可能会在处理它时传递它
  • 这取决于你想对输出做什么。在大多数情况下,您会希望使用.get(),因此您使用的是简单的原生 JavaScript 数组而不是 jQuery 对象。在.map() 之后的.get() 调用是惯用的jQuery。
  • 也就是说,嵌套的.map() 调用实际上应该省略内部的.get() 调用。我还没有玩弄它来为自己找出答案。
猜你喜欢
  • 2013-11-26
  • 1970-01-01
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
相关资源
最近更新 更多