Java xml遍历
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java xml遍历相关的知识,希望对你有一定的参考价值。
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>
你没说清楚运行是到底会发生什么错误,因为解析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.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来筛选节点类型,具体代码我没法给你,我一贴代码,百度就提示超过了最大字数,无语。。。如果你已经会弄了就这样吧,当真需要代码私下找我吧
请问 怎么样私下找您
追答你留个邮箱就ok啦,我把代码发过去
追问794815519@谢谢
参考技术Ajava xml遍历主要是使用java提供工具类DocumentBuilderFactory
类来解析xml文件和遍历,如下代码:
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();
参考技术B //打印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);
追问
大神,你写的这些我看不懂,我只能根据老师讲的再结合课本去编程,不能用别的方法
参考技术C 错在没有把xml贴出来追问关键不会贴xml
java xml节点添加修改属性
<first>
<second>
<here id="a" name="b">
<."还有一些子节点".>
</here>
<here id="c" name="d">
<...>
</here>
</second>
</first>
就是让<here>里面加个东西变成<here id="a" name="b" color="q">...
用的是
import org.xml.sax.SAXException;
dom4j
Document doc = reader.read(new FileInputStream("d.xml"));
Element root = doc.getRootElement();
List<Element> list = root.selectNodes("//here");
for (Element e : list)
System.out.println(e);
e.addAttribute("color", "q");
// 保存
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"));
writer.write(doc);
writer.close();本回答被提问者采纳
以上是关于Java xml遍历的主要内容,如果未能解决你的问题,请参考以下文章