java怎么在xml文件中保存和读取字符串数组
我使用jdom读写xml文件,想把一个字符串数组如:Stringweek[]={"Monday","Tuesday","Wednesday","Thursday","Fr...
我使用jdom读写xml文件,想把一个字符串数组如:String week[] ={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
保存到一个xml文件中,当需要时读出这个xml文件数据保存到一个字符串数组中该怎么处理?不用jdom用别的第三方jar也可以,请高手给一个例子,谢谢! 展开
保存到一个xml文件中,当需要时读出这个xml文件数据保存到一个字符串数组中该怎么处理?不用jdom用别的第三方jar也可以,请高手给一个例子,谢谢! 展开
推荐于2017-10-11 · 知道合伙人互联网行家
关注
展开全部
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class XmlTest {
public String[] xml2Array(String fileName) throws JDOMException, IOException{
SAXBuilder b = new SAXBuilder();
Document xmlFile = b.build(new File(fileName));
Element root = xmlFile.getRootElement();
List ls = root.getChildren("week");
String weeks[]=new String[7];
for( int i = 0;i<ls.size();i++){
Element e = (Element)ls.get(i);
String week = e.getText();
weeks[i]=week;
}
return weeks;
}
public void array2Xml(String[] weekArray,String fileName) throws FileNotFoundException, IOException{
Element weeks = new Element("weeks");
Document doc = new Document(weeks);
for(int i=0;i<weekArray.length;i++){
Element week = new Element("week");
week.setText(weekArray[i]);
weeks.addContent(week);
}
XMLOutputter out = new XMLOutputter();
out.setFormat(out.getFormat().setEncoding("GBK"));
out.output(doc,new FileOutputStream( new File(fileName)));
}
/**
* @param args
* @throws IOException
* @throws JDOMException
*/
public static void main(String[] args) throws JDOMException, IOException {
XmlTest xml = new XmlTest();
String fileName = "week.xml";
String weeks[] ={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
xml.array2Xml(weeks,fileName);
String testWeeks[] = xml.xml2Array(fileName);
for(String s:testWeeks){
System.out.println(s);
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class XmlTest {
public String[] xml2Array(String fileName) throws JDOMException, IOException{
SAXBuilder b = new SAXBuilder();
Document xmlFile = b.build(new File(fileName));
Element root = xmlFile.getRootElement();
List ls = root.getChildren("week");
String weeks[]=new String[7];
for( int i = 0;i<ls.size();i++){
Element e = (Element)ls.get(i);
String week = e.getText();
weeks[i]=week;
}
return weeks;
}
public void array2Xml(String[] weekArray,String fileName) throws FileNotFoundException, IOException{
Element weeks = new Element("weeks");
Document doc = new Document(weeks);
for(int i=0;i<weekArray.length;i++){
Element week = new Element("week");
week.setText(weekArray[i]);
weeks.addContent(week);
}
XMLOutputter out = new XMLOutputter();
out.setFormat(out.getFormat().setEncoding("GBK"));
out.output(doc,new FileOutputStream( new File(fileName)));
}
/**
* @param args
* @throws IOException
* @throws JDOMException
*/
public static void main(String[] args) throws JDOMException, IOException {
XmlTest xml = new XmlTest();
String fileName = "week.xml";
String weeks[] ={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
xml.array2Xml(weeks,fileName);
String testWeeks[] = xml.xml2Array(fileName);
for(String s:testWeeks){
System.out.println(s);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询