「用java遍历xml」java集合的遍历
本篇文章给大家谈谈用java遍历xml,以及java集合的遍历对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、如何用java遍历出xml中每一个attributeValue的值
- 2、java 如何遍历xml并输出每个节点的每个属性?
- 3、Java xml遍历
- 4、求大虾help! 如何用java遍历n个xml文件,并取得xml文件的各个属性..如果有代码的话就更好了.跪谢~
如何用java遍历出xml中每一个attributeValue的值
递归读取法即可
package tool;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLTool
{
private static final String XML = "MyXml.xml";
private static Document getDoc ( String xml )
{
DocumentBuilderFactory dbf = null;
DocumentBuilder db = null;
Document doc = null;
try
{
dbf = DocumentBuilderFactory.newInstance ();
db = dbf.newDocumentBuilder ();
doc = null == xml ? db.newDocument () : db.parse (xml);
return doc;
}
catch (Exception e)
{
e.printStackTrace ();
}
return null;
}
private static void readXML ( String xml, String... looks )
{
Document doc = getDoc (xml);
recursionXML (doc, looks);
}
private static void recursionXML ( Node node, String... looks )
{
NodeList nl = node.getChildNodes ();
if (nl.getLength () == 0)
{
return;
}
for ( int i = 0; i nl.getLength (); i++ )
{
Node n = nl.item (i);
Node parentNode = n.getParentNode ();
NamedNodeMap nnm = parentNode.getAttributes ();
if (
looks.length != 0
parentNode.getNodeName ().equals (looks[0])
nnm.getNamedItem (looks[1]).getNodeValue ().equals (looks[2])
)
{
System.out.println (nnm.getNamedItem ("Status"));
}
recursionXML (n, looks);
}
}
public static void main ( String[] args )
{
System.out.println ("输入id号进行查询: (n/N 退出)");
Scanner scanner = new Scanner (System.in);
String[] looks = { "UseTicketResult", "TicketNo", "47" };
String line = null;
while (scanner.hasNextLine ())
{
line = scanner.nextLine ().trim ();
if ("n".equalsIgnoreCase (line))
{
scanner.close ();
break;
}
looks[2] = line;
readXML (XML, looks);
System.out.println ("继续输入: ");
}
}
}
java 如何遍历xml并输出每个节点的每个属性?
//打印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);
}
}
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));
}
}
}
求大虾help! 如何用java遍历n个xml文件,并取得xml文件的各个属性..如果有代码的话就更好了.跪谢~
jdom解析xml
下面是xml文件:
?xml version="1.0" encoding="UTF-8"?
persons
person perid="1001"
namelhu/name
age89/age
address安徽淮北/address
sex男/sex
/person
person perid="1002"
namewe/name
age56/age
address北京海淀/address
sex女/sex
/person
/persons
下面是解析上面的xml文件:
通过JDOM来解析,需要借助第三方的组件.jdom.jar,网上有1.0的版本下载
package cn.com.jdom;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.ProcessingInstruction;
import org.jdom.input.SAXBuilder;
import com.sun.xml.internal.bind.v2.runtime.Name;
/**
* jdom解析xml文件
*
* @author ly *
*/
public class JDomXML {
public JDomXML() {
}
/**
* 解析xml文件
* @param xmlFile
*/
public void parseXml(File xmlFile) {
SAXBuilder sax = new SAXBuilder();//在内存中建立一个sax文档模型
try {
Document xmlDom = sax.build(xmlFile);//创建文档
//获得文件的根元素
Element root = xmlDom.getRootElement();
System.out.println("根元素是:"+root.getName());
//获得根元素的子节点
List childList = root.getChildren();
Iterator listIt = childList.iterator();
while(listIt.hasNext()){
Element element = (Element)listIt.next();
System.out.println("孩子结点是:"+element.getName());
}
//获得第一个孩子结点
Element firstChild = (Element) childList.get(0);
//获得孩子结点的属性
List attrList = firstChild.getAttributes();
Iterator attrIt = attrList.iterator();
while(attrIt.hasNext()){
Attribute attr = (Attribute ) attrIt.next();
System.out.println("第一个元素的属性是:"+attr.getName());
//获得属性的值
System.out.println("属性的值是:"+attr.getValue());
//获得属性的类型
System.out.println("属性的类型是:"+attr.getAttributeType());
}
List sonList = firstChild.getChildren();
Iterator sonIt = sonList.iterator();
while(sonIt.hasNext()){
Element temp = (Element)sonIt.next();
System.out.println("属性"+temp.getName()+"的值是:"+temp.getValue());
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JDomXML test = new JDomXML();
test.parseXml(new File("persons.xml"));
}
}
用java遍历xml的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java集合的遍历、用java遍历xml的信息别忘了在本站进行查找喔。
发布于:2022-11-24,除非注明,否则均为
原创文章,转载请注明出处。