xml文件为:
<?xml version="1.0" encoding="UTF-8"?>
<HD>
<disk name="C" name1="C1">
<capacity>8G</capacity>
<directories>200</directories>
<files>1580</files>
</disk>
<disk name="D" name1="D2">
<capacity>10G</capacity>
<directories>500</directories>
<files>3000</files>
</disk>
</HD>
我快要疯了 这里编辑不了xml文件,所有xml显示的有问题,根节点为HD,2个父节点为disk,有2个属性name,name1
子节点为capacity,directories,files
1.sax读取
package com.xml;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class saxReadXml extends DefaultHandler {
public void parserXMLFile(String fileName) throws SAXException, IOException {
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(this);
reader.setErrorHandler(this);
InputSource source = new InputSource(new FileInputStream(new File(
fileName)));
reader.parse(source);
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
System.out.print("<" + name);
for (int i = 0; i < attributes.getLength(); i++) {
String attrName = attributes.getQName(i);
String attrValue = attributes.getValue(i);
System.out.print(" " + attrName + "=" + attrValue);
}
System.out.print(">");
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
System.out.print("</" + name + ">");
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.print(new String(ch, start, length));
}
@Override
public void startDocument() throws SAXException {
System.out.println("<xml version=\"1.0\" encoding=\"utf-8\"?>");
}
public static void main(String[] args) throws SAXException, IOException {
saxReadXml parser = new saxReadXml();
parser.parserXMLFile("src/com/xml/test.xml");
}
}
2.jdk读取
package com.xml;
//jdk自己读取xml文件
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class jdkReakXml {
// Document可以看作是XML在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对
// 内存的操作来实现对XML的操作,首先第一步获取XML相关的Document
public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
Document doc = db.parse("src/com/xml/test.xml");
Element element = doc.getDocumentElement();
System.out.println("根节点为:" + element.getTagName());// 获得根元素HD
NodeList nodelist = element.getElementsByTagName("disk");// 获得父元素disk
System.out.println("父节点为:" + nodelist.item(0).getNodeName());
System.out.println("父节点disk的个数:" + nodelist.getLength());
System.out.println("_____________________");
for (int i = 0; i < nodelist.getLength(); i++) {
NamedNodeMap map = (NamedNodeMap) nodelist.item(i)
.getAttributes();
for (int j = 0; j < map.getLength(); j++) {
Node node = map.item(j);
System.out.println("父节点的属性名:" + node.getNodeName() + ","
+ "父节点的属性值:" + node.getNodeValue());
}
NodeList childNodes = nodelist.item(i).getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
// 如果这个节点属于Element ,再进行取值
if (childNode instanceof Element) {
// System.out.println("子节点名为:"+childNode.getNodeName()+"相对应的值为"+childNode.getFirstChild().getNodeValue());
System.out.println("子节点名:" + childNode.getNodeName()
+ "," + "相对应值:"
+ childNode.getFirstChild().getNodeValue());
}
}
System.out.println("_____________________");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.jdom读取
package com.xml;
//用jdom读取xml文件
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class jdomReadXml {
public static void main(String[] args) throws Exception {
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(jdomReadXml.class.getClassLoader()
.getResourceAsStream("com/xml/test.xml")); // 构造文档对象
Element root = doc.getRootElement(); // 获取根元素HD
System.out.println("根节点为:" + root.getName());
List<Element> list = root.getChildren("disk");// 取名字为disk的所有元素
System.out.println("父节点为:" + list.get(0).getName());
System.out.println("父节点disk的个数:" + list.size());
System.out.println("_____________________");
for (int i = 0; i < list.size(); i++) {
Element element = (Element) list.get(i);
List<Attribute> att = element.getAttributes();
for (int j = 0; j < att.size(); j++) {
Attribute attribute = att.get(j);
System.out.println("父节点属性名:" + attribute.getName() + ","
+ "属性值:" + attribute.getValue());
}
List<Element> childNode = element.getChildren();
for (int j = 0; j < childNode.size(); j++) {
System.out.println("子节点名:" + childNode.get(j).getName() + ","
+ "相对应值:" + childNode.get(j).getValue());
}
System.out.println("_____________________");
}
}
}
4.dom4j读取
package com.xml;
import java.io.File;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ProcessingInstruction;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;
public class dom4jReadXml {
public static void main(String[] args) {
SAXReader reader = new SAXReader();
File file = new File("src/com/xml/test.xml");
try {
Document doc = reader.read(file);
doc.accept(new MyVistor());
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static class MyVistor extends VisitorSupport {
public void visit(Attribute node) {
System.out.println("属性名:" + node.getName() + "=" + node.getValue());
}
public void visit(Element node) {
if (node.isTextOnly()) {
System.out.println("子节点:" + node.getName() + "="
+ node.getText());
} else {
System.out.println("--------" + node.getName() + "-------");
}
}
@Override
public void visit(ProcessingInstruction node) {
System.out.println("PI:" + node.getTarget() + " " + node.getText());
}
}
}