在 Java 中,XML 用org.w3c.dom.Document
对象表示。 在本 XML 教程中,我们将学习:
- 将 XML 字符串转换为 XML 文档
- 将 XML 文件内容转换为 XML 文档
1)将字符串转换为 XML 文档
要将将 XML 字符串转换为 XML Dom,我们需要以下类:
javax.xml.parsers.DocumentBuilder
:定义 API,以从来自各种输入源的 XML 内容中获取 XML DOM 文档实例。 这些输入源是InputStreams
,文件,URL 和 SAXInputSources
。javax.xml.parsers.DocumentBuilderFactory
:定义一种工厂 API,使应用能够获取解析器(DocumentBuilder
),该解析器从 XML 内容生成 DOM 对象树。org.w3c.dom.Document
:它表示整个 XML DOM。 从概念上讲,它是文档树的根,并通过工厂方法提供对文档数据的访问,甚至深入到树中。java.io.StringReader
:根据字符串内容创建流。DocumentBuilder
使用此流读取 XML 内容进行解析。
package com.howtodoinjava.demo;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class StringtoXMLExample
{
public static void main(String[] args)
{
final String xmlStr = "<employees>" +
" <employee id=\"101\">" +
" <name>Lokesh Gupta</name>" +
" <title>Author</title>" +
" </employee>" +
" <employee id=\"102\">" +
" <name>Brian Lara</name>" +
" <title>Cricketer</title>" +
" </employee>" +
"</employees>";
//Use method to convert XML string content to XML Document object
Document doc = convertStringToXMLDocument( xmlStr );
//Verify XML document is build correctly
System.out.println(doc.getFirstChild().getNodeName());
}
private static Document convertStringToXMLDocument(String xmlString)
{
//Parser that produces DOM object trees from XML content
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//API to obtain DOM Document instance
DocumentBuilder builder = null;
try
{
//Create DocumentBuilder with default configuration
builder = factory.newDocumentBuilder();
//Parse the content to Document object
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
return doc;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
//Output:
employees
2)将 XML 文件转换为 XML 文档
要使从 XML 文件获取 XML dom,而不是将 XML 字符串传递给DocumentBuilder
,请传递文件路径以使解析器直接读取文件内容。
我们有包含 XML 内容的employees.xml
文件,我们将阅读以获取 XML 文档。
<employees>
<employee id="101">
<name>Lokesh Gupta</name>
<title>Author</title>
</employee>
<employee id="102">
<name>Brian Lara</name>
<title>Cricketer</title>
</employee>
</employees>
package com.howtodoinjava.demo;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class StringtoXMLExample
{
public static void main(String[] args)
{
final String xmlFilePath = "employees.xml";
//Use method to convert XML string content to XML Document object
Document doc = convertXMLFileToXMLDocument( xmlFilePath );
//Verify XML document is build correctly
System.out.println(doc.getFirstChild().getNodeName());
}
private static Document convertXMLFileToXMLDocument(String filePath)
{
//Parser that produces DOM object trees from XML content
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//API to obtain DOM Document instance
DocumentBuilder builder = null;
try
{
//Create DocumentBuilder with default configuration
builder = factory.newDocumentBuilder();
//Parse the content to Document object
Document doc = builder.parse(new File(filePath));
return doc;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
//Output:
employees
将我的问题放在评论部分。
学习愉快!