---
title: "An invalid XML character (Unicode: 0x1) was found"
author: "popjxc"
date: "2010-09-27T13:20:29Z"
tags: ["GMF", "XML"]
categories: ["编程语言"]
slug: "an-invalid-xml-character-unicode-0x1-was-found"
draft: false
lastmod: "2011-07-19T12:42:35Z"
id: 773090
url: "https://popjxc.iteye.com/blog/773090"
---

GMF的model保存为xml时，如果含有一些不可见的字符，比如unicode的0x1, 就会保存失败。这并不是GMF的问题，而是一个xml规则定义的问题。因为这些字符在xml 

1.0的定义中是违法的，需要转义为&#x1;的形式。但如如果以xml 1.1的格式保存就不会出现这个问题，因为xml 1.1会自动进行转义。

在GMF生成的代码中的XXXDiagramEditorUtil类中对下面的方法进行修改:

```
public static Map<?, ?> getSaveOptions() {
		HashMap<String, Object> saveOptions = new HashMap<String, Object>();
		saveOptions.put(XMLResource.OPTION_ENCODING, "UTF-8"); 
		saveOptions.put(XMLResource.OPTION_XML_VERSION, "1.1");			//save xml in version 1.1
		return saveOptions;
	}
```

 

 

这是就可以保存为xml 1.1的格式了。

 

但是升级到xml 1.1后，会出现很多问题，建议手动转义特殊字符再保存。