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

}

}

相关推荐
YWL3 分钟前
OpenLayers + Vue 2 使用指南(01)
前端·javascript·vue.js
暖和_白开水24 分钟前
数据分析agent (七):contextvars 模块上下文request_id
java·前端·数据分析
程序员黑豆32 分钟前
鸿蒙应用开发:Flex 组件从入门到实战
前端·华为·harmonyos
大爱编程♡32 分钟前
Vue3+TypeScript+element-plus的文章管理项目(配后端接口)-退出及文章管理页面
前端·javascript·typescript
先吃饱再说34 分钟前
React 组件设计:从 Props 传递到组件封装
前端·react.js
独立开阀者_FwtCoder37 分钟前
最近做了一个健身小程序:智形健身助手,健身的佬们来提点意见
前端·javascript·github
舒灿40 分钟前
喵ing Tab 1.0.0 正式发布
前端·ai编程
葡萄城技术团队44 分钟前
当表格数据来自外部系统:SpreadJS 如何让异步公式一键刷新
前端
小徐_23331 小时前
Open Wot 1.0.5 发布:让 AI 接入 wot-ui,只需要两条命令
前端·uni-app·ai编程
Android研究员1 小时前
Android进阶之事件分发机制深度剖析
android·前端·面试