java domparser,异常与java DOM解析器

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)

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值