我正在使用 java 创建站点地图 xml。
Element img = doc.createElement("image:image");
我得到一个例外:
org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.
关于如何创建元素的任何建议?
我认为您想使用 XML 命名空间。有关如何执行此操作的方法,请参阅此问题:使用 Java 中的名称空间创建 XML 文档
在这个元素名称"image:image"
中,这:
不是一个有效的 cahrecter。
编辑
Best Naming Practices for XML Element Name
Make names descriptive. Names with an underscore separator are nice: <first_name>, <last_name>.
Names should be short and simple, like this: <book_title> not like this: <the_title_of_the_book>.
Avoid "-" characters. If you name something "first-name," some software may think you want to subtract name from first.
Avoid "." characters. If you name something "first.name," some software may think that "name" is a property of the object "first."
Avoid ":" characters. Colons are reserved to be used for something called namespaces (more later).
XML documents often have a corresponding database. A good practice is to use the naming rules of your database for the elements in the XML documents.
Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software vendor doesn't support them.