

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# util.xml 中的 XML 帮助程序
<a name="xml-helpers-in-util-xml-js"></a>

 `util.xml` 包含帮助进行 XML 字符串转换的方法。

## util.xml 实用程序列表
<a name="xml-helpers-in-util-xml-list-js"></a>

 **`util.xml.toMap(String) : Object`**  
将 XML 字符串转换为字典。  
**示例 1：**  

```
Input:

<?xml version="1.0" encoding="UTF-8"?>
<posts>
<post>
    <id>1</id>
    <title>Getting started with GraphQL</title>
</post>
</posts>

Output (object):

{
    "posts":{
      "post":{
        "id":1,
        "title":"Getting started with GraphQL"
      }
    }
}
```
**示例 2：**  

```
Input:

<?xml version="1.0" encoding="UTF-8"?>
<posts>
<post>
  <id>1</id>
  <title>Getting started with GraphQL</title>
</post>
<post>
  <id>2</id>
  <title>Getting started with AppSync</title>
</post>
</posts>

Output (JavaScript object):

{
    "posts":{
    "post":[
        {
            "id":1,
            "title":"Getting started with GraphQL"
        },
        {
            "id":2,
            "title":"Getting started with AppSync"
        }
    ]
    }
}
```

**`util.xml.toJsonString(String, Boolean?) : String`**  
将 XML 字符串转换为 JSON 字符串。这与 `toMap` 类似，只不过输出是字符串。如果您要直接转换 XML 响应并将其从 HTTP 对象返回到 JSON，这非常有用。您可以设置一个可选的布尔值参数，以确定是否要对 JSON 进行字符串编码。