0

I am using dom pasrer to create xml document where tag name start with digit. and it is giving exception. seems with java DOM parser , it is not allowed to have tagname starting with digit.

Same thing ,It is achievable in C#(dot-net) using System.Xml;

is there any way, i can achieve the same.

below is the more progrom and output:

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)

1 Answer 1

1

The Java DOM parser is correct to reject these tags.

XML tag names should not begin with a digit, but may contain a digit. This is specified in the Extensible Markup Language (XML) 1.1 (Second Edition) document as follows:

[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)*
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.