java读取xml文件,结果写入新建的txt中。

import java.io.File;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;public class TestDom {...
java读取xml文件,结果写入新建的txt中。
读取xml文件用jdom.jar
写文件:看你是想原封不动的写到TXT中还是按照你想要得格式进行流写入到文本中。2013-05-03
import java.io.File;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;public class TestDom {public static void main(String[] args) {System.out.println(countMoney());}public static int countMoney(){int money=0;Set<Integer> set =new HashSet<Integer>();DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();DocumentBuilder db;try {db = dbf.newDocumentBuilder();Document doc=db.parse(new File("F:\\orders.xml"));Element root=doc.getDocumentElement();NodeList childs=root.getChildNodes();for(int i=0;i<childs.getLength();i++){if(childs.item(i).getNodeName().equals("order")){NodeList orderSons=childs.item(i).getChildNodes();for(int j=0;j<orderSons.getLength();j++){if(orderSons.item(j).getNodeName().equals("item")){NodeList itemSons=orderSons.item(j).getChildNodes();int n=0;int p=0;for(int k=0;k<itemSons.getLength();k++){if(itemSons.item(k).getNodeName().equals("qty")){n=Integer.parseInt(itemSons.item(k).getFirstChild().getNodeValue());}if(itemSons.item(k).getNodeName().equals("price")){p=Integer.parseInt(itemSons.item(k).getFirstChild().getNodeValue());}if(n*p!=0){money=n*p;set.add(money);}}}}}}}catch (Exception e) {e.printStackTrace();}if(set!=null){money=0;for(Iterator<Integer> it=set.iterator();it.hasNext();){money+=(int)it.next();}}return money;}}  以前做的练习,不过大同小异。别手懒了,动手试一试。 把上面的改改,得到你想要的内容,用流写入txt就行了。不会再问。 
2013-05-03
使用sax或dom的方式进行读取,然后通过流的形式保存到指定txt文本内。2013-05-03
mengvlog 阅读 80 次 更新于 2025-10-28 22:52:19 我来答关注问题0
  • anonymous 如何用Java实现对xml文件的读取和写入以及保存

    public class Dom4jSample { public static void main(String[] args) { Dom4jSample dom4jSample = new Dom4jSample();Document document = dom4jSample.createDocument();try{ dom4jSample.FileWrite(document);Document documentStr = dom4jSample.StringToXML("I Love!");dom4jSample....

  • 1.读取XML文件,获得document对象。SAXReader reader = new SAXReader();Document document = reader.read(new File("input.xml"));2.解析XML形式的文本,得到document对象。String text = "";Document document = DocumentHelper.parseText(text);3.主动创建document对象。Document do...

  • anonymous java读取XML文件

    ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes("utf-8")); SAXReader xmlReader = new SAXReader(); Document document = xmlReader.read(in); Element root = document.getRootElement(); XPath x = document.createXPath("//ShowList/Movie"); List

  • 建议看哈JAXP 关于java操作xml其实很简单

  •  深空见闻 java将xml文件转成字符串

    一、使用FileInputStream和BufferedReader类 这是一种直接且常用的方法,通过读取XML文件的每一行并将其拼接成字符串。具体实现如下:使用FileInputStream打开XML文件。使用BufferedReader逐行读取文件内容。将读取到的每一行追加到StringBuilder对象中。读取完成后,将StringBuilder对象转换为字符串返回。二、使用...

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部