Java XML打印_Java 打印XML

A below xml is received:

1

900

1

1

2

400

2

5

9

5

100

3

Please write a java program to read and parse the xml file, outputthe mobile information.

Java 打印XML

package test;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class parseXML {

public static void parseXml(String fileName) {

try {

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

Document document = db.parse(fileName);

NodeList nodeList = document.getChildNodes();

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

Node mobilePhone = nodeList.item(i);

NodeList mobileNodeList = mobilePhone.getChildNodes();

for (int j = 0; j < mobileNodeList.getLength(); j++) {

Node node = mobileNodeList.item(j);

NamedNodeMap nameNodeMap = node.getAttributes();

for (int k = 0; nameNodeMap != null && k < nameNodeMap.getLength(); k++) {

System.out.println(nameNodeMap.item(k).getNodeName() + ":"

+ nameNodeMap.item(k).getTextContent());

}

NodeList employeeMeta = node.getChildNodes();

for (int k = 0; employeeMeta != null

&& k < employeeMeta.getLength(); k++) {

System.out.println(employeeMeta.item(k).getNodeName()

+ ":" + employeeMeta.item(k).getTextContent());

}

}

}

} catch (Exception e) {

System.out.println(e.getMessage());

}

}

public static void main(String[] args) {

parseXml("src/config.xml");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值