java读取xml内容并转换成java对象
时间: 2024-05-06 09:20:17 浏览: 99
可以使用Java中的DOM或SAX解析器来读取XML内容并将其转换为Java对象。
DOM解析器将整个XML文档加载到内存中,允许您对文档进行修改和查询。以下是使用DOM解析器读取XML内容并将其转换为Java对象的示例代码:
```java
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 XmlToObjectConverter {
public static void main(String[] args) {
try {
File inputFile = new File("example.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("student");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String name = element.getElementsByTagName("name").item(0).getTextContent();
int age = Integer.parseInt(element.getElementsByTagName("age").item(0).getTextContent());
String gender = element.getElementsByTagName("gender").item(0).getTextContent();
Student student = new Student(name, age, gender);
System.out.println(student);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Student {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
@Override
public String toString() {
return "Name: " + name + ", Age: " + age + ", Gender: " + gender;
}
}
```
SAX解析器是一种事件驱动的解析器,它逐行读取XML文档并触发相应的事件。它比DOM解析器更快,但不允许您修改XML文档。以下是使用SAX解析器读取XML内容并将其转换为Java对象的示例代码:
```java
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class XmlToObjectConverter {
public static void main(String[] args) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean bName = false;
boolean bAge = false;
boolean bGender = false;
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("name")) {
bName = true;
}
if (qName.equalsIgnoreCase("age")) {
bAge = true;
}
if (qName.equalsIgnoreCase("gender")) {
bGender = true;
}
}
public void characters(char ch[], int start, int length) throws SAXException {
if (bName) {
String name = new String(ch, start, length);
bName = false;
}
if (bAge) {
int age = Integer.parseInt(new String(ch, start, length));
bAge = false;
}
if (bGender) {
String gender = new String(ch, start, length);
bGender = false;
Student student = new Student(name, age, gender);
System.out.println(student);
}
}
};
saxParser.parse(new File("example.xml"), handler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Student {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
@Override
public String toString() {
return "Name: " + name + ", Age: " + age + ", Gender: " + gender;
}
}
```
阅读全文