

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# \$1util.xml 中的 XML 協助程式
<a name="xml-helpers-in-utils-xml"></a>

**注意**  
我們現在主要支援 APPSYNC\$1JS 執行期及其文件。請考慮[在此處](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-reference-js-version.html)使用 APPSYNC\$1JS 執行期及其指南。

`$util.xml` 包含協助程式方法，可讓您更輕鬆地將 XML 回應轉譯為 JSON 或字典。

## \$1util.xml utils 清單
<a name="xml-helpers-in-utils-xml-list"></a>

****`$util.xml.toMap(String) : Map`****  
將 XML 字串轉換為字典。  

```
Input:

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

Output (JSON representation):

{
  "posts":{
    "post":{
      "id":1,
      "title":"Getting started with GraphQL"
    }
  }
}


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 AWS AppSync</title>
</post>
</posts>

Output (JSON representation):

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

****`$util.xml.toJsonString(String) : String`****  
將 XML 字串轉換為 JSON 字串。這類似於 *toMap*，但輸出是字串。如果要將來自 HTTP 物件的 XML 回應直接轉換並傳回給 JSON，這非常實用。

****`$util.xml.toJsonString(String, Boolean) : String`****  
將 XML 字串轉換為具有選用布林值參數的 JSON 字串，以判斷您是否要對 JSON 進行字串編碼。