forked from wujun728/jun_java_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJAXPAttr.java
More file actions
96 lines (69 loc) · 2.22 KB
/
JAXPAttr.java
File metadata and controls
96 lines (69 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
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;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
/**
* 忽略Text的空白 测试无效
* @author licheng
*
*/
public class JAXPAttr {
/**
*主函数
*/
public static void main(String[] args) {
GiveData3 give=new GiveData3();
try {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true); //忽略空白缩进
DocumentBuilder domParser=factory.newDocumentBuilder();
InputStream in = TestDom.class.getClassLoader().getResourceAsStream("university.xml"); //读取src目录下文件
Document document=domParser.parse(in);
Element root=document.getDocumentElement();
NodeList nodeList=root.getChildNodes();
give.output(nodeList);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class GiveData3{
public void output(NodeList nodelist){
int size=nodelist.getLength(); //获取接点列表的长度
for(int k=0;k<size;k++){
Node node=nodelist.item(k); //获取节点列表中的一项
if(node.getNodeType()==node.TEXT_NODE){ //节点类型为TEXT
Text textNode=(Text)node;
String content=textNode.getWholeText();
System.out.print(content);
}
if(node.getNodeType()==Node.ELEMENT_NODE){ //节点类型为ELEMENT
Element elementNode=(Element)node;
String name=elementNode.getNodeName();
System.out.print(name);
NamedNodeMap map=elementNode.getAttributes(); //获取属性节点集合
/**
* 属性节点操作
*/
for(int m=0;m<map.getLength();m++){
Attr attrNode=(Attr)map.item(m);
String attrName=attrNode.getName(); //属性名称
String attrValue=attrNode.getValue(); //属性值
System.out.print(" "+attrName+"="+attrValue);
}
NodeList nodes=elementNode.getChildNodes();
output(nodes); //递归掉用该方法
}
}
}
}