java 将字符串转换为 w3c.dom.Element: XMLParseException:Start of root element expected

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5910288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:24:19  来源:igfitidea点击:

Convert a String to w3c.dom.Element: XMLParseException:Start of root element expected

javaxml

提问by Pedantic

I found the following piece of code from a blog, when running it I get an exception

我从博客中找到了以下代码,运行时出现异常

XMLParseException:Start of root element expected. at 9th line.

XMLParseException: 预期根元素的开始。在第 9 行。

Can any one explain why I get the Exception and suggest any other way for converting String to an element?

任何人都可以解释为什么我得到异常并建议将 String 转换为元素的任何其他方法吗?

String s = "Hello DOM Parser";
java.io.InputStream sbis = new java.io.StringBufferInputStream(s);
javax.xml.parsers.DocumentBuilderFactory b = javax.xml.parsers.DocumentBuilderFactory.newInstance();
b.setNamespaceAware(false);
org.w3c.dom.Document doc = null;
javax.xml.parsers.DocumentBuilder db = null;
db = b.newDocumentBuilder();
doc = db.parse(sbis);     

org.w3c.dom.Element e = doc.getDocumentElement();

回答by Thizzer

Like said in the comment, "Hello DOM Parser" is not a XML element. And so the parser doesn't know what to do with it. I don't know what kind document you are building, but if you want HTML you can embed the text in a html tag for example;

就像评论中所说的那样,“Hello DOM Parser”不是 XML 元素。所以解析器不知道如何处理它。我不知道你正在构建什么样的文档,但如果你想要 HTML,你可以将文本嵌入到 html 标签中;

<div>Hello DOM Parser</div>
<span>Hello DOM Parser</span>

if you are building XML, you can embed the text in any random html tag;

如果您正在构建 XML,您可以将文本嵌入到任何随机的 html 标签中;

<mytag>Hello DOM Parser</mytag>

Some explanation on DOM; http://www.w3.org/DOM

关于DOM的一些解释; http://www.w3.org/DOM

To answer your question, to convert a String to a w3c Element, you can use createElement;

要回答您的问题,要将 String 转换为 w3c 元素,您可以使用 createElement;

Element hello = document.createElement("hello");          
hello.appendChild(document.createTextNode("Hello DOM Parser"));

This results in;

这导致;

<hello>Hello DOM Parser</hello>

回答by kostja

To create a DOM Elementwith a custom tag (which I assume is what you want, but can't be sure), you can use the following approach:

要创建Element带有自定义标记的 DOM (我认为这是您想要的,但不能确定),您可以使用以下方法:

String customTag = "HelloDOMParser";

Document doc = documentBuilder.newDocument();       

String fullName = nameSpacePrefix + ":" + customTag;

Element customElement = document.createElementNS(namespaceUri, fullName);

doc.appendChild(customElement);

I am assuming you know the namespace URI and your prefix (if any). If you don't use namespaces, just use the createElement()method instead.

我假设您知道命名空间 URI 和您的前缀(如果有)。如果您不使用命名空间,请改用该createElement()方法。

回答by James Jithin

The parse method of DocumentBuilder accept the input stream which contains the xml content. The following change will work for you:

DocumentBuilder 的 parse 方法接受包含 xml 内容的输入流。以下更改对您有用:

String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root><elem1>Java</elem1></root>";

Try to avoid using the deprecated classes such as StringBufferInputStream. You can refer to the document below to know more about Java XML parsing.

尽量避免使用已弃用的类,例如 StringBufferInputStream。您可以参考下面的文档来了解更多关于 Java XML 解析的信息。

http://www.java-samples.com/showtutorial.php?tutorialid=152

http://www.java-samples.com/showtutorial.php?tutorialid=152