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

}

}

相关推荐
graceyun12 分钟前
C语言进阶习题【1】指针和数组(4)——指针笔试题3
android·java·c语言
上官熊猫15 分钟前
nuxt3项目打包部署到服务器后配置端口号和开启https
前端·vue3·nuxt3
我科绝伦(Huanhuan Zhou)16 分钟前
Linux 系统服务开机自启动指导手册
java·linux·服务器
旦沐已成舟1 小时前
K8S-Pod的环境变量,重启策略,数据持久化,资源限制
java·docker·kubernetes
S-X-S1 小时前
项目集成ELK
java·开发语言·elk
Ting-yu1 小时前
项目实战--网页五子棋(游戏大厅)(3)
java·java-ee·maven·intellij-idea
dal118网工任子仪2 小时前
61,【1】BUUCTF WEB BUU XSS COURSE 11
前端·数据库·xss
约定Da于配置4 小时前
uniapp封装websocket
前端·javascript·vue.js·websocket·网络协议·学习·uni-app
山楂树の4 小时前
xr-frame 模型摆放与手势控制,支持缩放旋转
前端·xr·图形渲染
LBJ辉5 小时前
1. 小众但非常实用的 CSS 属性
前端·css