📄 domparser.java
字号:
/**
*
*/
import java.io.IOException;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
/**
* @author Teness
*
*/
public class DomParser {
public void parser(String FileName){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
doc=builder.parse(FileName);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
doc.normalize();
NodeList nl = doc.getChildNodes();
for (int i = 0; i < nl.getLength(); i++)
{
printAttribute(nl.item(i));
}
}
public void printAttribute(Node node){
Node temp = node;
if (!temp.getNodeName().equals("#comment") && !temp.getNodeName().equals("#text")){
NamedNodeMap attr = temp.getAttributes();
if (attr != null)
{
for (int i = 0; i < attr.getLength(); i++)
{
System.out.print(attr.item(i).getNodeName()+" = "+attr.item(i).getNodeValue()+" ");
}
}
NodeList children = temp.getChildNodes();
if (children != null)
{
for (int i = 0; i < children.getLength() ; i++)
{
printAttribute(children.item(i));
}
}
System.out.println();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DomParser dp = new DomParser();
dp.parser("xmlfile.xml");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -