XML 中的数组定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6098922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:46:30  来源:igfitidea点击:

Array definition in XML?

xmlarrays

提问by Nick

In XML, how do I declare array of integers?

在 XML 中,如何声明整数数组?

I can declare it as the following:

我可以声明如下:

<numbers type="array">
    <value>3</value>
    <value>2</value>
    <value>1</value>
</numbers>

but may be there's simpler way like this?

但可能有更简单的方法吗?

<numbers [3,2,1]></numbers>

采纳答案by carlosfigueira

The second way isn't valid XML; did you mean <numbers>[3,2,1]</numbers>?

第二种方式不是有效的 XML;你的意思是<numbers>[3,2,1]</numbers>

If so, then the first one is preferred because all you need to get the array elements is some XML manipulation. On the second one you first need to get the value of the <numbers> element via XML manipulation, then somehow parse the [3,2,1]text using something else.

如果是这样,则首选第一个,因为获取数组元素所需的只是一些 XML 操作。在第二个中,您首先需要通过 XML 操作获取 <numbers> 元素的值,然后以某种方式[3,2,1]使用其他东西解析文本。

Or if you really want some compact format, you can consider using JSON (which "natively" supports arrays). But that depends on your application requirements.

或者如果你真的想要一些紧凑的格式,你可以考虑使用 JSON(它“本机”支持数组)。但这取决于您的应用程序要求。

回答by Lars

No, there is no simpler way. You only can lose the type=array.

不,没有更简单的方法。你只能失去type=array.

<numbers>
    <value>3</value>
    <value>2</value>
    <value>1</value>
</numbers>

回答by Stepan Stepanov

Once I've seen such an interesting construction:

一旦我看到了这样一个有趣的结构:

<Ids xmlns:id="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        <id:int>1787</id:int>
</Ids>

回答by Eric

As its name is "numbers" it is clear it is a list of number... So an array of number... no need of the attribute type... Although I like the principle of specifying the type of field in a type attribute...

因为它的名字是“数字”,很明显它是一个数字列表......所以一个数字数组......不需要属性类型......虽然我喜欢在类型中指定字段类型的原则属性...