java中如何读取xml中数据。多节点的。给我一个例子,谢谢。
3个回答
展开全部
xml文件
<?xml version="1.0" encoding="GBK"?>
<root>
<!--<rarPath>rar</rarPath>-->
<rarPath>C:/Program Files/WinRAR/WinRAR.exe</rarPath>
<zipPass>run1234!@#</zipPass>
<!-- 生成xml文件路径 暂时没用-->
<rootPath name="/"/>
<filePath>
<!-- 上机日志文件路径 -->
<online name="online/"/>
<!-- 下机日志文件路径 -->
<offline name="offline/"/>
</filePath>
</root>
java 代码
public static void main(String[] args) throws Exception{
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("c:/config.xml");
Document doc = builder.build(xmlFile);
Element root = doc.getRootElement();
//根路径
Element element = root.getChild("rootPath");
System.out.println(element.getAttributeValue("name"));
//rar命令路径
element = root.getChild("rarPath");
System.out.println(element.getText());
//zip压缩文件密码
element = root.getChild("zipPass");
System.out.println(element.getText());
//日志文件路径
element = root.getChild("filePath");
List<Element> elementList = element.getChildren();
for (Element e : elementList) {
System.out.println(e.getAttributeValue("name"));
}
}
<?xml version="1.0" encoding="GBK"?>
<root>
<!--<rarPath>rar</rarPath>-->
<rarPath>C:/Program Files/WinRAR/WinRAR.exe</rarPath>
<zipPass>run1234!@#</zipPass>
<!-- 生成xml文件路径 暂时没用-->
<rootPath name="/"/>
<filePath>
<!-- 上机日志文件路径 -->
<online name="online/"/>
<!-- 下机日志文件路径 -->
<offline name="offline/"/>
</filePath>
</root>
java 代码
public static void main(String[] args) throws Exception{
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("c:/config.xml");
Document doc = builder.build(xmlFile);
Element root = doc.getRootElement();
//根路径
Element element = root.getChild("rootPath");
System.out.println(element.getAttributeValue("name"));
//rar命令路径
element = root.getChild("rarPath");
System.out.println(element.getText());
//zip压缩文件密码
element = root.getChild("zipPass");
System.out.println(element.getText());
//日志文件路径
element = root.getChild("filePath");
List<Element> elementList = element.getChildren();
for (Element e : elementList) {
System.out.println(e.getAttributeValue("name"));
}
}
展开全部
SAXReader reader = new SAXReader();
Document document = reader.read("src/dom4j/test.xml");
Element rootelement = document.getRootElement();
Iterator iter = rootelement.elementIterator();
Element element;
StringBuffer str = new StringBuffer();
while (iter.hasNext()) {
element = (Element) iter.next();
System.out.println(element.getName());
}
Document document = reader.read("src/dom4j/test.xml");
Element rootelement = document.getRootElement();
Iterator iter = rootelement.elementIterator();
Element element;
StringBuffer str = new StringBuffer();
while (iter.hasNext()) {
element = (Element) iter.next();
System.out.println(element.getName());
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File(SAVEFILENAME);
if (file.exists()) {
Document document = db.parse(SAVEFILENAME);
NodeList good = document.getElementsByTagName("Good");
ArrayList<Goods> list = new ArrayList<Goods>();
for (int i = 0; i < good.getLength(); i++) {
Element element = (Element) good.item(i);
Goods goodObj = new Goods();
// 获取数据
String id = element.getElementsByTagName("Id")
.item(0).getFirstChild().getNodeValue();
String name = element.getElementsByTagName("Name").item(0)
.getFirstChild().getNodeValue();
String price = element.getElementsByTagName(
"Price").item(0).getFirstChild()
.getNodeValue();
String unit = element.getElementsByTagName("Unit")
.item(0).getFirstChild().getNodeValue();
String count = element.getElementsByTagName("Count")
.item(0).getFirstChild().getNodeValue();
// 封装数据
int id1=Integer.parseInt(id);
goodObj.setId(id1);
goodObj.setName(name);
double price1=Double.valueOf(price);
goodObj.setPrice(price1);
goodObj.setUnit(unit);
int count1=Integer.parseInt(count);
goodObj.setCount(count1);
// 存入集合
list.add(goodObj);
}
return list;
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (ParserConfigurationException e) {
System.out.println(e.getMessage());
} catch (SAXException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
return null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File(SAVEFILENAME);
if (file.exists()) {
Document document = db.parse(SAVEFILENAME);
NodeList good = document.getElementsByTagName("Good");
ArrayList<Goods> list = new ArrayList<Goods>();
for (int i = 0; i < good.getLength(); i++) {
Element element = (Element) good.item(i);
Goods goodObj = new Goods();
// 获取数据
String id = element.getElementsByTagName("Id")
.item(0).getFirstChild().getNodeValue();
String name = element.getElementsByTagName("Name").item(0)
.getFirstChild().getNodeValue();
String price = element.getElementsByTagName(
"Price").item(0).getFirstChild()
.getNodeValue();
String unit = element.getElementsByTagName("Unit")
.item(0).getFirstChild().getNodeValue();
String count = element.getElementsByTagName("Count")
.item(0).getFirstChild().getNodeValue();
// 封装数据
int id1=Integer.parseInt(id);
goodObj.setId(id1);
goodObj.setName(name);
double price1=Double.valueOf(price);
goodObj.setPrice(price1);
goodObj.setUnit(unit);
int count1=Integer.parseInt(count);
goodObj.setCount(count1);
// 存入集合
list.add(goodObj);
}
return list;
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (ParserConfigurationException e) {
System.out.println(e.getMessage());
} catch (SAXException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
return null;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询