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 小时前
什么?还不知道git cherry pick?
前端·javascript·git
白兰地空瓶4 小时前
🏒 前端 AI 应用实战:用 Vue3 + Coze,把宠物一键变成冰球运动员!
前端·vue.js·coze
Liu.7745 小时前
vue3使用vue3-print-nb打印
前端·javascript·vue.js
韩立学长5 小时前
【开题答辩实录分享】以《自助游网站的设计与实现》为例进行选题答辩实录分享
java·mysql·spring
ss2735 小时前
线程池:任务队列、工作线程与生命周期管理
java·后端
不像程序员的程序媛5 小时前
Spring的cacheEvict
java·后端·spring
SAP小崔说事儿5 小时前
在数据库中将字符串拆分成表单(SQL和HANA版本)
java·数据库·sql·sap·hana·字符串拆分·无锡sap
凌云若寒6 小时前
半导体代加工企业标签模板痛点的全景式解决方案
java
松涛和鸣6 小时前
Linux Makefile : From Basic Syntax to Multi-File Project Compilation
linux·运维·服务器·前端·windows·哈希算法
dly_blog6 小时前
Vue 逻辑复用的多种方案对比!
前端·javascript·vue.js