org.w3c.dom解析xml

一直用的dom4j解析,最近碰到工程里面是这种解析方式。

先记录下网上百度的一段代码。

 

写道
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import javax.xml.parsers.*;
import org.w3c.dom.*;
public class edit
{
public static void main(String[] args)
{
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("links.xml");
doc.normalize();

NodeList books =doc.getDocumentElement().getChildNodes();
Node book;
NodeList lists;

book=books.item(1);

//添加节点
Element id=doc.createElement("id");
id.appendChild(doc.createTextNode("first"));
book.appendChild(id);
//删除节点
book.removeChild(book.getChildNodes().item(1));

//修改节点
Text sina=doc.createTextNode("sina");
book.getChildNodes().item(2).replaceChild(sina,book.getChildNodes().item(2).getFirstChild());

System.out.println(book.getChildNodes().item(2).getNodeName());
System.out.println(books.getLength());
System.out.println(book.getChildNodes().getLength());
//浏览查看
for(int i=1;i<books.getLength();i++)
{
book=books.item(i);
lists=book.getChildNodes();
for(int j=1;j<lists.getLength();j++)
{
if(lists.item(j).getNodeType()==Node.ELEMENT_NODE)
System.out.println(lists.item(j).getFirstChild().getNodeValue());
}
}

TransformerFactory tFactory =TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new java.io.File("links.xml"));
transformer.transform(source, result);
}catch(Exception e){System.out.println(e);}
}
}

 -----------------------------------------------------------------------------------------------------

有如下的xml文档

<?xml version=”1.0”?>

<font>

        <name>Helvetica</name>

        <size>36</size>

</font>

遍历整个xml的所有node

 

 

 

XML文档时,解析器会得到5个结果:

写道
import java.io.File;

import java.io.IOException;


import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;


import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;


public class XMLReader {


public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException

{

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

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

//将xml文件解释成文档对象

Document doc = db.parse(file);

//取得文档根

doc.normalize();

Element root = doc.getDocumentElement();

//取得要元素名

System.out.println("The root element is:" + root.getNodeName());

//获取孩子节点

NodeList children = root.getChildNodes();

stepThrough(root);

}



private static void stepThrough (Node start)

{

for(Node child = start.getFirstChild();child != null;child = child.getNextSibling())

{

if(child instanceof Element)//去除多余的空白

{

System.out.print("节点名:"+child.getNodeName());

System.out.println("\t节点值:"+ child.getNodeValue());

}

if(child != null)

stepThrough(child);

}

}

}

 在处理这个

 

<font><name>之间的空白区域

Name元素

</name><size>之间的空白区域

Size元素

</size></font>之间的空白区域

如果只希望得到子元素,那么你可以忽略空白字符:

 

 

xml文档节点

写道
for(int i = 0;i < children.getLength(); i++)

{

Node child = children.item(i);

if (child instanceof Element)

{

Element childElement = (Element)child;

......

}

}

 遍历整个

 

for(Node childNode = element.getFirstChild();childNode != null;childNode = childNode.getNextSibling())

{

        ......

}

遍历整个xml某节点的所有属性

 

 

 

写道
NameNodeMap attributes = element.getAttributes();

for (int i = 0;i<attributes.getLength();i++)

{

Node attribute = attributes.item(i);

String name = attribute.getNodeName();//获得属性名

String value = attribute.getNodeValue();//获得属性值

}

 如果只想知道己知属性的值,只需用

 

String unit = element.getAttribute(“unit”);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值