反序列化XML文档是将XML格式的数据转换为可以使用的编程语言对象或数据结构的过程。在许多编程语言中,都有现成的库或模块可以帮助开发者完成这个任务。以下是一些常见编程语言中如何反序列化XML文档的示例:
Python中可以使用内置的xml.etree.ElementTree模块来解析和反序列化XML文档。以下是一个简单的示例:
import xml.etree.ElementTree as ET
xml_data = '''<?xml version="1.0"?><catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies and an evil sorceress.</description>
</book>
</catalog>
'''
root = ET.fromstring(xml_data)
for book in root.findall('book'):
print(book.attrib['id'])
print(book.find('title').text)
print(book.find('author').text)
Java中可以使用内置的javax.xml.parsers.DocumentBuilderFactory和javax.xml.parsers.DocumentBuilder类来解析和反序列化XML文档。以下是一个简单的示例:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class XmlParser {
public static void main(String[] args) {
String xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
<catalog>" +
" <book id=\"bk101\">" +
" <author>Gambardella, Matthew</author>" +
" <title>XML Developer's Guide</title>" +
" <genre>Computer</genre>" +
" <price>44.95</price>" +
" <publish_date>2000-10-01</publish_date>" +
" <description>An in-depth look at creating applications with XML.</description>" +
" </book>" +
" <book id=\"bk102\">" +
" <author>Ralls, Kim</author>" +
" <title>Midnight Rain</title>" +
" <genre>Fantasy</genre>" +
" <price>5.95</price>" +
" <publish_date>2000-12-16</publish_date>" +
" <description>A former architect battles corporate zombies and an evil sorceress.</description>" +
" </book>" +
"</catalog>";
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
InputStream is = new ByteArrayInputStream(xmlData.getBytes(StandardCharsets.UTF_8));
Document doc = dBuilder.parse(is);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("book");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Book id: " + eElement.getAttribute("id"));
System.out.println("Title: " + eElement.getElementsByTagName("title").item(0).getTextContent());
System.out.println("Author: " + eElement.getElementsByTagName("author").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
在JavaScript中,可以使用内置的DOMParser对象来解析和反序列化XML文档。以下是一个简单的示例:
const xmlData = `<?xml version="1.0"?><catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies and an evil sorceress.</description>
</book>
</catalog>
`;
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlData, "application/xml");
const books = xmlDoc.getElementsByTagName("book");
for (let i = 0; i< books.length; i++) {
const book = books[i];
console.log("Book id: " + book.getAttribute("id"));
console.log("Title
领取专属 10元无门槛券
手把手带您无忧上云