我正在使用dom pasrer创建标签名称以数字开头的xml文档。 这是例外。对于java DOM解析器,似乎不允许将标记名以数字开头。
同样的事情,使用System.Xml;
可以在C#(dot-net)中实现有什么办法,我可以达到同样的目的。
以下是更多的进展和输出:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class WriteXMLFile {
public static void main(String argv[]) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("company");
doc.appendChild(rootElement);
Element firstname = doc.createElement("1name");
firstname.appendChild(doc.createTextNode("yong"));
rootElement.appendChild(firstname);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
}
}
例外:
Exception in thread "main" org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.createElement(CoreDocumentImpl.java:618)
at com.impetus.avatar.WriteXMLFile.main(WriteXMLFile.java:25)
答案 0 :(得分:1)
Java DOM解析器拒绝这些标记是正确的。
XML标记名称不应以开头,但可以包含数字。这在Extensible Markup Language (XML) 1.1 (Second Edition)文档中指定如下:
[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] |
[#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] |
[#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] |
[#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] |
[#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] |
#xB7 | [#x0300-#x036F] | [#x203F-#x2040]
[5] Name ::= NameStartChar (NameChar)*