java解析返回xml数据
时间: 2023-08-31 10:06:17 浏览: 100
在Java中解析XML数据通常使用Java API for XML Processing(JAXP),它是Java语言中用于XML处理的标准API之一。JAXP提供了DOM解析器和SAX解析器。
DOM解析器会将整个XML文档加载到内存中作为一个树形结构,可以通过遍历树来访问XML文档中的各个元素和属性。以下是使用DOM解析器解析XML数据的示例代码:
```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;
import java.io.File;
public class XMLParser {
public static void main(String argv[]) {
try {
File inputFile = new File("input.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("employee");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Employee id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstName").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastName").item(0).getTextContent());
System.out.println("Email : " + eElement.getElementsByTagName("email").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
另外,SAX解析器是一种基于事件驱动的解析器,它逐行解析XML文档,可以在解析过程中处理文档中的各个元素和属性。使用SAX解析器需要实现org.xml.sax.ContentHandler接口的方法,以下是使用SAX解析器解析XML数据的示例代码:
```java
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
public class XMLParser extends DefaultHandler {
boolean bFirstName = false;
boolean bLastName = false;
boolean bEmail = false;
boolean bSalary = false;
public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("employee")) {
String id = attributes.getValue("id");
System.out.println("Employee id : " + id);
} else if (qName.equalsIgnoreCase("firstName")) {
bFirstName = true;
} else if (qName.equalsIgnoreCase("lastName")) {
bLastName = true;
} else if (qName.equalsIgnoreCase("email")) {
bEmail = true;
} else if (qName.equalsIgnoreCase("salary")) {
bSalary = true;
}
}
public void characters(char ch[], int start, int length) throws SAXException {
if (bFirstName) {
System.out.println("First Name: " + new String(ch, start, length));
bFirstName = false;
} else if (bLastName) {
System.out.println("Last Name: " + new String(ch, start, length));
bLastName = false;
} else if (bEmail) {
System.out.println("Email: " + new String(ch, start, length));
bEmail = false;
} else if (bSalary) {
System.out.println("Salary: " + new String(ch, start, length));
bSalary = false;
}
}
public static void main(String argv[]) {
try {
File inputFile = new File("input.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XMLParser userhandler = new XMLParser();
saxParser.parse(inputFile, userhandler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
以上是两种常见的解析XML数据的方法,具体使用哪种方法取决于项目需求和个人喜好。
阅读全文