Java xml遍历
importjava.util.*;importjavax.xml.parsers.*;importorg.w3c.dom.*;publicclassFilem{publ...
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class Filem {
public static void main(String[] args) throws Exception {
show();
}
public static void show() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("FilemMessage.xml");
NodeList list = doc.getElementsByTagName("filems");
List l = new ArrayList();
for (int i = 0; i < list.getLength(); i++) {
if (list.item(i).equals(null)) {
break;
} else {
Element e = (Element) list.item(i).getChildNodes().item(i);
String name = e.getAttribute("name");
String Englishname = e.getAttribute("Englishname");
String direct = e.getAttribute("direct");
String actor = e.getAttribute("actor");
String type = e.getAttribute("type");
String price = e.getAttribute("price");
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));
}
}
}
我到底哪里错了
<?xml version="1.0" encoding="UTF-8"?>
<filems>
<filem id="1">
<name>非常完美</name>
<Englishname>perfect</Englishname>
<direct>尹萌</direct>
<actor>范冰冰</actor>
<type>Romance</type>
<price>60</price>
<time>09:00</time>
</filem>
<filem id="2">
<name>非常完美</name>
<Englishname>perfect</Englishname>
<direct>尹萌</direct>
<actor>范冰冰</actor>
<type>Romance</type>
<price>60</price>
<time>13:00</time>
</filem>
</filems> 展开
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class Filem {
public static void main(String[] args) throws Exception {
show();
}
public static void show() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("FilemMessage.xml");
NodeList list = doc.getElementsByTagName("filems");
List l = new ArrayList();
for (int i = 0; i < list.getLength(); i++) {
if (list.item(i).equals(null)) {
break;
} else {
Element e = (Element) list.item(i).getChildNodes().item(i);
String name = e.getAttribute("name");
String Englishname = e.getAttribute("Englishname");
String direct = e.getAttribute("direct");
String actor = e.getAttribute("actor");
String type = e.getAttribute("type");
String price = e.getAttribute("price");
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));
}
}
}
我到底哪里错了
<?xml version="1.0" encoding="UTF-8"?>
<filems>
<filem id="1">
<name>非常完美</name>
<Englishname>perfect</Englishname>
<direct>尹萌</direct>
<actor>范冰冰</actor>
<type>Romance</type>
<price>60</price>
<time>09:00</time>
</filem>
<filem id="2">
<name>非常完美</name>
<Englishname>perfect</Englishname>
<direct>尹萌</direct>
<actor>范冰冰</actor>
<type>Romance</type>
<price>60</price>
<time>13:00</time>
</filem>
</filems> 展开
展开全部
你没说清楚运行是到底会发生什么错误,因为解析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));
}
}
}
更多追问追答
追问
请问 为什么要加final
我需要的是filems子节点下面节点的属性,上面给出了xml文档,老师上课的时候也这样说过的
追答
不加final也可以,这就是一种个人习惯,这里无关紧要。
你应该使用3层for循环来遍历,遍历时需要使用node.getNodeType() == Node.ELEMENT_NODE来筛选节点类型,具体代码我没法给你,我一贴代码,百度就提示超过了最大字数,无语。。。如果你已经会弄了就这样吧,当真需要代码私下找我吧
展开全部
java xml遍历主要是使用java提供工具类DocumentBuilderFactory
类来解析xml文件和遍历,如下代码:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.*;
import java.io.File;
public class TraverseXML
{
public static void main(String[] args)
{
try{
String file = "TestData\\test.xml";
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new File(file));
NodeList nodes = doc.getElementsByTagName("person");
System.out.println("总共有"+nodes.getLength()+"个人。");
for(int i=0;i<nodes.getLength();i++)
{
Node node = nodes.item(i);
NodeList childNodes = node.getChildNodes();
System.out.println("person有"+childNodes.getLength()+"个节点。");
for(int j=0;j<childNodes.getLength();j++)
{
Node childNode = childNodes.item(j);
if(childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getNodeName().equals("name"))
System.out.println("名字:"+childNode.getFirstChild().getNodeValue());
if(childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getNodeName().equals("age"))
System.out.println("年龄:"+childNode.getFirstChild().getNodeValue());
}
System.out.println();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//打印xml文档
private void parseElement(Element root)
{
//System.out.print(root.getNamespaceURI());
System.out.print("<");
System.out.print(root.getNodeName());
//System.out.print(root.getPrefix());
//System.out.print(":");
//System.out.print(root.getLocalName());
NamedNodeMap nnm = root.getAttributes();
for(int i = 0; i < nnm.getLength(); i++)
{
Attr attr = (Attr)nnm.item(i);
System.out.print(" ");
System.out.print(attr.getName());
System.out.print("=\"");
System.out.print(attr.getValue());
System.out.print("\"");
}
System.out.print(">");
NodeList list = root.getChildNodes();
for(int i = 0; i < list.getLength(); i++)
{
Node node = list.item(i);
if(node instanceof Element)
{
Element e = (Element)node;
parseElement(e);
}
else if(node instanceof Text)
{
Text t = (Text)node;
System.out.print(t.getNodeValue());
}
}
System.out.print("</");
System.out.print(root.getNodeName());
System.out.print(">");
}
private void parseRootName()
{
Element root = doc.getDocumentElement();
System.out.println(root.getNodeName());
}
//工厂
private void getDocument()
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder db = factory.newDocumentBuilder();
doc = db.parse(new File(fileName));
}
catch(Exception ex)
{
ex.printStackTrace();
System.exit(1);
}
}
private void parseElement(Element root)
{
//System.out.print(root.getNamespaceURI());
System.out.print("<");
System.out.print(root.getNodeName());
//System.out.print(root.getPrefix());
//System.out.print(":");
//System.out.print(root.getLocalName());
NamedNodeMap nnm = root.getAttributes();
for(int i = 0; i < nnm.getLength(); i++)
{
Attr attr = (Attr)nnm.item(i);
System.out.print(" ");
System.out.print(attr.getName());
System.out.print("=\"");
System.out.print(attr.getValue());
System.out.print("\"");
}
System.out.print(">");
NodeList list = root.getChildNodes();
for(int i = 0; i < list.getLength(); i++)
{
Node node = list.item(i);
if(node instanceof Element)
{
Element e = (Element)node;
parseElement(e);
}
else if(node instanceof Text)
{
Text t = (Text)node;
System.out.print(t.getNodeValue());
}
}
System.out.print("</");
System.out.print(root.getNodeName());
System.out.print(">");
}
private void parseRootName()
{
Element root = doc.getDocumentElement();
System.out.println(root.getNodeName());
}
//工厂
private void getDocument()
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder db = factory.newDocumentBuilder();
doc = db.parse(new File(fileName));
}
catch(Exception ex)
{
ex.printStackTrace();
System.exit(1);
}
}
追问
大神,你写的这些我看不懂,我只能根据老师讲的再结合课本去编程,不能用别的方法
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
错在没有把xml贴出来
追问
关键不会贴xml
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |