import javautilList;
import orgdom4jDocument;
import orgdom4jDocumentException;
import orgdom4jElement;
import orgdom4jioSAXReader;
public class XmlTester {
public static void main(String[] args) throws DocumentException {
// 使用了dom4j解析xml
// 读取目录下用来测试的testxml文件,取得xml主内容
Document document = new SAXReader()read("src/abc/testxml")getDocument();
int i = 1;
// 遍历文档根节点(wuxialist)下的子节点列表,即txtbook节点的集合
for(Element txtbook : (List<Element>)documentgetRootElement()elements()){
//取得txtbook节点下的name节点的内容
Systemoutprintln(i+""+txtbookelement("name")getText());
i++; //原来这里少些了这一行,先补上
}
}
}
//数据都帮你取出来完了,摆控件赋值自个弄弄就好了
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemXml;
namespace XMLTest
{
class Program
{
static void Main(string[] args)
{
XmlDocument xml = new XmlDocument();
xmlLoad("aaxml");
XmlNode xe = xmlSelectSingleNode("Information/SoftWare/Browsers");
foreach (XmlNode item in xeChildNodes)
{
if (itemName == "Browser")
{
if (itemSelectSingleNode("Name")InnerText == "Mozilla Firefox")
{
ConsoleWriteLine(itemSelectSingleNode("Name")InnerText);
ConsoleWriteLine(itemSelectSingleNode("Version")InnerText);
ConsoleWriteLine(itemSelectSingleNode("HomePage")InnerText);
XmlNode searchEnginesNode = itemSelectSingleNode("SearchEngines");
foreach (XmlNode itemEngine in searchEnginesNodeChildNodes)
{
ConsoleWriteLine(itemEngineSelectSingleNode("Name")InnerText);
}
}
else if (itemSelectSingleNode("Name")InnerText == "Google Chrome")
{
ConsoleWriteLine(itemSelectSingleNode("Name")InnerText);
ConsoleWriteLine(itemSelectSingleNode("Version")InnerText);
ConsoleWriteLine(itemSelectSingleNode("HomePage")InnerText);
XmlNode searchEnginesNode = itemSelectSingleNode("SearchEngines");
foreach (XmlNode itemEngine in searchEnginesNodeChildNodes)
{
ConsoleWriteLine(itemEngineSelectSingleNode("Name")InnerText);
}
}
}
}
ConsoleReadKey();
}
}
}
给个例子你, 自己去套用就行给你
<xml version="10" encoding="utf-8"><Accounts>
<Account type="type1">
<code>100001</code>
<pass>123</pass>
<name>李四</name>
<money>100000000</money>
</Account>
<Account type="type2">
<code>100002</code>
<pass>123</pass>
<name>张三</name>
<money>100000</money>
</Account>
</Accounts>
java代码解析:
import javaio;/
xml文件解析
@author young
/
import javaxxmlparsersDocumentBuilder;
import javaxxmlparsersDocumentBuilderFactory;
import orgw3cdomDocument;
import orgw3cdomElement;
import orgw3cdomNode;
import orgw3cdomNodeList;
public class XmlExam {
public static void main(String args[]) {
Element element = null;
// 可以使用绝对路劲
File f = new File("xmlxml");
// documentBuilder为抽象不能直接实例化(将XML文件转换为DOM文件)
DocumentBuilder db = null;
DocumentBuilderFactory dbf = null;
try {
// 返回documentBuilderFactory对象
dbf = DocumentBuilderFactorynewInstance();
// 返回db对象用documentBuilderFatory对象获得返回documentBuildr对象
db = dbfnewDocumentBuilder();
// 得到一个DOM并返回给document对象
Document dt = dbparse(f);
// 得到一个elment根元素
element = dtgetDocumentElement();
// 获得根节点
Systemoutprintln("根元素:" + elementgetNodeName());
// 获得根元素下的子节点
NodeList childNodes = elementgetChildNodes();
// 遍历这些子节点
for (int i = 0; i < childNodesgetLength(); i++) {
// 获得每个对应位置i的结点
Node node1 = childNodesitem(i);
if ("Account"equals(node1getNodeName())) {
// 如果节点的名称为"Account",则输出Account元素属性type
Systemoutprintln("\r\n找到一篇账号 所属区域: "
+ node1getAttributes()getNamedItem("type")
getNodeValue() + " ");
// 获得<Accounts>下的节点
NodeList nodeDetail = node1getChildNodes();
// 遍历<Accounts>下的节点
for (int j = 0; j < nodeDetailgetLength(); j++) {
// 获得<Accounts>元素每一个节点
Node detail = nodeDetailitem(j);
if ("code"equals(detailgetNodeName())) // 输出code
Systemout
println("卡号: " + detailgetTextContent());
else if ("pass"equals(detailgetNodeName())) // 输出pass
Systemout
println("密码: " + detailgetTextContent());
else if ("name"equals(detailgetNodeName())) // 输出name
Systemout
println("姓名: " + detailgetTextContent());
else if ("money"equals(detailgetNodeName())) // 输出money
Systemout
println("余额: " + detailgetTextContent());
}
}
}
} catch (Exception e) {
eprintStackTrace();
}
}
}
以上就是关于JAVA 读取XML文件全部的内容,包括:JAVA 读取XML文件、将xml多个相同下节点的分类获取 C#、java jdom获取xml的所有子节点包括节点里的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)