遍历xml删选_java 使用 xml 查找遍历

package mypack;

import java.io.File;

import java.io.IOException;

import java.util.List;

import org.jdom.*;

import org.jdom.input.*;

import org.jdom.xpath.XPath;

public class test {

public static void main(String[] args) {

File f=new File("test.xml");

SAXBuilder saxb=new SAXBuilder();

try {

Document xmldoc=saxb.build(f);

XPath xpath=XPath.newInstance("//LINE[编号='001']");//创建一个查询编号为001的LINE元素的XPATH。

List nodes=xpath.selectNodes(xmldoc);//进行查询

System.out.println("返回记录数:"+nodes.size());

for(int i=0;i

System.out.print(i+" / ");

System.out.println(((Element)nodes.get(i)).getChildText("名称"));//返回该LINE的名称的文本值。

}

} catch (JDOMException e) {

e.printStackTrace();

} catch (IOException 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);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值