简体   繁体   English

Java DOM解析器异常

[英]Exception with java DOM parser

I am using dom pasrer to create xml document where tag name start with digit. 我正在使用dom pasrer创建标签名称以digit开头的xml文档。 and it is giving exception. 它给了例外。 seems with java DOM parser , it is not allowed to have tagname starting with digit. 似乎与Java DOM解析器一起使用,不允许以数字开头的标记名。

Same thing ,It is achievable in C#(dot-net) using System.Xml; 同样,使用System.Xml在C#(dot-net)中可以实现;

is there any way, i can achieve the same. 有什么办法,我可以达到同样的效果。

below is the more progrom and output: 以下是更多的progrom和输出:

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 : 例外情况:

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)

The Java DOM parser is correct to reject these tags. Java DOM解析器拒绝这些标签是正确的。

XML tag names should not begin with a digit, but may contain a digit. XML标签名称不应以数字开始 ,但可能含有一个数字。 This is specified in the Extensible Markup Language (XML) 1.1 (Second Edition) document as follows: 可扩展标记语言(XML)1.1(第二版)文档中对此进行了指定,如下所示:

[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)*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM