第一使用org.w3c.dom.Document类解析
package com.xml.dom;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* @author 熊浪
* @Email [email protected]
* @Time 2017年10月17日 @此类的作用:
*/
public class DOMReaderXml {
public String useDomReaderXML(String path, String key) throws IOException {
URL url = this.getClass().getResource(path);// 生成path的url
try {
File file = new File(url.getFile());
if (!file.exists()) {
throw new IOException("文件不存在");
}
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder doucmentBuilder = documentFactory.newDocumentBuilder();
Document document = doucmentBuilder.parse(file);
NodeList list = document.getElementsByTagName("value");
for (int i = 0, len = list.getLength(); i < len; i++) {
System.out.print(document.getElementsByTagName("name").item(i).getFirstChild().getNodeValue());
System.out.print("\t");
System.out.print(document.getElementsByTagName("namevalue").item(i).getFirstChild().getNodeValue());
System.out.println();
}
} catch (ParserConfigurationException | SAXException e) {
e.printStackTrace();
}
return key;
}
public static void main(String[] args) {
try {
new DOMReaderXml().useDomReaderXML("/META-INF/base.xml", "name");
} catch (IOException e) {
e.printStackTrace();
}
}
}
效果图:
第二种,使用org.jdom.Document解析
package com.xml.jdom;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
/**
* @author 熊浪
* @Email [email protected]
* @Time 2017年10月17日 @此类的作用:
*/
@SuppressWarnings("unchecked")
public class JDOMReaderXml {
public String useJDOMReadXML(String path, String key) throws IOException {
URL url = this.getClass().getClassLoader().getResource(path);
File file = new File(url.getFile());
if (!file.exists()) {
throw new IOException("文件不存在");
}
SAXBuilder saxBuilder = new SAXBuilder();
try {
Document document = saxBuilder.build(file);
Element element = document.getRootElement();
List<Element> list = element.getChildren();
for (int i = 0, len = list.size(); i < len; i++) {
System.out.print(list.get(i).getChildTextTrim("name"));
System.out.print("\t");
System.out.print(list.get(i).getChildTextTrim("namevalue"));
System.out.println();
}
} catch (JDOMException e) {
e.printStackTrace();
}
return key;
}
public static void main(String[] args) {
try {
new JDOMReaderXml().useJDOMReadXML("META-INF/base.xml", "name");
} catch (IOException e) {
e.printStackTrace();
}
}
}
第三种,使用org.dom4j.Document解析
package com.xml.sax;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.helpers.DefaultHandler;
/**
* @author 熊浪
* @Email [email protected]
* @Time 2017年10月17日 @此类的作用:
*/
@SuppressWarnings("unchecked")
public class SAXReaderXml extends DefaultHandler {
public String useSAXReadXML(String path, String key) throws IOException {
if (path.startsWith("/"))
path = path.substring(1);
URL url = this.getClass().getClassLoader().getResource(path);
File file = new File(url.getFile());
if (!file.exists()) {
throw new IOException("文件不存在");
}
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(file);
Element element = document.getRootElement();
List<Element> list = element.elements();
for (Element value : list) {
List<Element> childs = value.elements();
for (Element child : childs) {
System.out.println(child.getName() + "\t" + child.getText());
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
return key;
}
public static void main(String[] args) {
try {
new SAXReaderXml().useSAXReadXML("META-INF/base.xml", "name");
} catch (IOException e) {
e.printStackTrace();
}
}
}
效果图