在 Java 中,如何将 XML 解析为字符串而不是文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/562160/
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-08-11 16:15:29  来源:igfitidea点击:

In Java, how do I parse XML as a String instead of a file?

javaxmlstringfileparsing

提问by Dewayne

I have the following code:

我有以下代码:

DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);

How can I get it to parse XML contained within a String instead of a file?

如何让它解析包含在字符串而不是文件中的 XML?

采纳答案by shsteimer

I have this function in my code base, this should work for you.

我的代码库中有这个功能,这应该对你有用。

public static Document loadXMLFromString(String xml) throws Exception
{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);
}

also see this similar question

也看到这个类似的问题

回答by duffymo

javadocsshow that the parse method is overloaded.

javadocs显示 parse 方法被重载。

Create a StringStream or InputSource using your string XML and you should be set.

使用您的字符串 XML 创建一个 StringStream 或 InputSource ,您应该被设置。

回答by Uri

One way is to use the version of parse that takes an InputSource rather than a file

一种方法是使用采用 InputSource 而不是文件的 parse 版本

A SAX InputSource can be constructed from a Reader object. One Reader object is the StringReader

可以从 Reader 对象构造 SAX InputSource。一个 Reader 对象是 StringReader

So something like

所以像

parse(new InputSource(new StringReader(myString))) may work. 

回答by Akbar ibrahim

Convert the string to an InputStream and pass it to DocumentBuilder

将字符串转换为 InputStream 并将其传递给 DocumentBuilder

final InputStream stream = new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
builder.parse(stream);

EDIT
In response to bendin's comment regarding encoding, see shsteimer's answer to this question.

编辑
在回答bendin的有关编码评论,看到shsteimer的回答了这个问题。

回答by Yasir Shabbir Choudhary

I'm using this method

我正在使用这种方法

public Document parseXmlFromString(String xmlString){
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputStream inputStream = new    ByteArrayInputStream(xmlString.getBytes());
    org.w3c.dom.Document document = builder.parse(inputStream);
    return document;
}

回答by Shukant Pal

You can use the Scilca XML Progession package available at GitHub.

您可以使用 GitHub 上提供的 Scilca XML Progession 包。

XMLIterator xi = new VirtualXML.XMLIterator("<xml />");
XMLReader xr = new XMLReader(xi);
Document d = xr.parseDocument();

回答by lgb

just input

只需输入

this.file = File("your xml file path")
this.document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file)