「java循环xml」java循环语句案例
今天给各位分享java循环xml的知识,其中也会对java循环语句案例进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java中 XML文件的的一部分怎么循环
String xml =" kc22" +
" aka0631/aka063" +
" aka0651/aka065" +
" akc2201/akc220" +
" akc2212014-11-03/akc221" ;
for (int i=0; i=10; i++) //循环10次
{
xml += " akc2221/akc222" ;
}
xml += " akc2231/akc223" +
" akc2241/akc224" +
" akc2250.0/akc225" +
" akc2260.0/akc226" +
" akc2290/akc229" +
" zka1001/zka100" +
" /kc22";
java操作xml
document = DocumentHelper.parseText(infoXML);
Element root = document.getRootElement(); //根节点
Iterator it =root.elements().iterator();
while(it.hasNext()){
........................
}
然后循环的去获取你自定义的节点名称获得相应的值。
这是DOM方法
百度不允许发代码,如果有不懂的可以HI我!
Java xml遍历
你没说清楚运行是到底会发生什么错误,因为解析XML这玩意和XML本身的格式有关,你应该把XML也给出。我只能假设你的XML是这种形式:
?xml version="1.0" encoding="UTF-8" ?
root
filems name="a1" Englishname="a2" direct="a3" actor="a4" type="a5"
price="a6" time="a7" /
filems name="b1" Englishname="b2" direct="b3" actor="b4" type="b5"
price="b6" time="b7" /
/root
这样运行你的代码会报NulPointerExceptoin,应该把Element e = (Element) list.item(i).getChildNodes().item(i);
去掉,你的代码需要改成这样子:
import java.util.ArrayList;
import java.util.List;
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 Filem {
public static void main(final String[] args) throws Exception {
show();
}
public static void show() throws Exception {
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
final DocumentBuilder db = dbf.newDocumentBuilder();
final Document doc = db.parse("FilemMessage.xml");
final NodeList list = doc.getElementsByTagName("filems");
final List l = new ArrayList();
final int length = list.getLength();
for (int i = 0; i length; i++) {
final Element e = (Element) list.item(i);
if (e == null) {
break;
}
final String name = e.getAttribute("name");
final String Englishname = e.getAttribute("Englishname");
final String direct = e.getAttribute("direct");
final String actor = e.getAttribute("actor");
final String type = e.getAttribute("type");
final String price = e.getAttribute("price");
final String time = e.getAttribute("time");
l.add(name);
l.add(Englishname);
l.add(direct);
l.add(actor);
l.add(type);
l.add(price);
l.add(time);
}
for (int j = 0; j l.size(); j++) {
System.out.println(l.get(j));
}
}
}
关于java循环xml和java循环语句案例的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。