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();

}

}

相关推荐
西西学代码2 小时前
Flutter---Stream
java·服务器·flutter
几何心凉3 小时前
openGauss:多核时代企业级数据库的性能与高可用新标杆
前端·数据库·数据库开发
Blossom.1184 小时前
移动端部署噩梦终结者:动态稀疏视觉Transformer的量化实战
java·人工智能·python·深度学习·算法·机器学习·transformer
静若繁花_jingjing5 小时前
IDEA下载
java·ide·intellij-idea
AiXed5 小时前
PC微信协议之AES-192-GCM算法
前端·数据库·python
代码丰5 小时前
函数式接口+default接口+springAi 中的ducumentReader去理解为什么存在default接口的形式
java
AllData公司负责人5 小时前
实时开发平台(Streampark)--Flink SQL功能演示
大数据·前端·架构·flink·开源
小满zs5 小时前
Next.js第五章(动态路由)
前端
清沫5 小时前
VSCode debugger 调试指南
前端·javascript·visual studio code
一颗宁檬不酸6 小时前
页面布局练习
前端·html·页面布局