xml parser

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.NamedNodeMap;

import org.xml.sax.InputSource;

import java.io.StringReader;

public class XMLParser {

public static void main(String\[\] args) {

try {

//File inputFile = new File("input.xml"); // XML文件路径

String xmlString = "<?xml version=\"1.0\"?>\n" +

"<class>\n" +

" <student rollno=\"393\">\n" +

" <firstname>Doe</firstname>\n" +

" <lastname>John</lastname>\n" +

" <nickname>DJ</nickname>\n" +

" <marks>85</marks>\n" +

" </student>\n" +

" <student rollno=\"493\">\n" +

" <firstname>Jane</firstname>\n" +

" <lastname>Doe</lastname>\n" +

" <nickname>JD</nickname>\n" +

" <marks>95</marks>\n" +

" </student>\n" +

"</class>";

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

// Document doc = dBuilder.parse(inputFile);

InputSource is = new InputSource(new StringReader(xmlString));

Document doc = dBuilder.parse(is);

doc.getDocumentElement().normalize();

System.out.println("Root element: " + doc.getDocumentElement().getNodeName());

traverseNodes(doc.getDocumentElement(), 0); // 从根节点开始遍历

} catch (Exception e) {

e.printStackTrace();

}

}

public static void traverseNodes(Node node, int level) {

// 打印节点的属性

if (node.hasAttributes()) {

NamedNodeMap attributes = node.getAttributes();

for (int i = 0; i < attributes.getLength(); i++) {

Node attr = attributes.item(i);

System.out.println(repeatString(" ", level * 2) + "Attribute: " + attr.getNodeName() + " = " + attr.getNodeValue());

}

}

// 打印节点的内容(如果有内容且不是只有空白字符)

String content = node.getTextContent().trim();

if (!content.isEmpty() && node.getChildNodes().getLength() == 1) {

System.out.println(repeatString(" ", level * 2) + "Content: " + content);

}

// 递归遍历子节点

NodeList nodeList = node.getChildNodes();

for (int i = 0; i < nodeList.getLength(); i++) {

Node currentNode = nodeList.item(i);

if (currentNode.getNodeType() == Node.ELEMENT_NODE) {

System.out.println(repeatString(" ", level * 2) + "Node Name: " + currentNode.getNodeName());

traverseNodes(currentNode, level + 1);

}

}

}

public static String repeatString(String str, int count) {

StringBuilder sb = new StringBuilder();

for (int i = 0; i < count; i++) {

sb.append(str);

}

return sb.toString();

}

}

相关推荐
Joy T11 分钟前
聊天系统中五大ID的演进与实战解析
java·前端·人工智能·spring·agent入门
JAVA面经实录91722 分钟前
线上故障标准处理流程【完整版|面试背诵+生产落地】
java·面试·职场和发展
殷色玫瑰33 分钟前
C++入门基础复习:从命名空间到引用与nullptr,一篇重新捡回C++基础
java·开发语言·c++
Seraphina362 小时前
DVWA(SQL注入-low,medium,XSS反射-low)
前端·数据库·笔记·sql·网络安全·web·xss
Access开发易登软件2 小时前
Access 导入 Markdown 表格:把文档里的数据写进表
前端·html·vba·markdown·access开发
大侠归来2 小时前
C语言素数判断:从入门到进阶
java·c语言·算法
谢亮_vipxieliang2 小时前
Spring Boot 自动配置原理:从 @SpringBootApplication 到自定义 Starter
java·spring boot·后端
朝朝辞暮i2 小时前
C++ 第 25 课:getter / setter + this
java·javascript·c++
扶风ff2 小时前
求职笔试资料太散?用练题簿在线刷题,按岗位整理练习清单
前端·学习·小程序
蜗牛互联网2 小时前
Python接入Gemini 3.8 Flash实现票据视觉抽取与规则校验
java·javascript·网络·人工智能·python