DocumentBuilderFactory解析xml
来源:互联网 发布:js获取当前时间格转换 编辑:程序博客网 时间:2024/06/01 10:47
使用DocumentBuilderFactory来解析xml文件
首先是主函数代码:
public static void main(String[] args) { try { final Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(Main.class.getResourceAsStream("/actions.xml")); Reader reader = new Reader(); reader.load(new Entry(xml.getDocumentElement())); } catch (final IOException e) { } catch (final SAXException e) { }catch (final ParserConfigurationException e) { } }
这里使用了DocumentBuilderFactory::newInstance()获取了一个DocumentBuilderFactory实例。
然后调用DocumentBuilderFactory::newDocumentBuilder()官方文档的描述如下:
Creates a new instance of aDocumentBuilder using the currently configured parameters.
然后使用DocumentBuilder::parse(String uri) parse一个xml文件,结果是返回一个Document实例,同时这个方法需要捕捉IO和SAX异常,
其中的SAXException时parse发生错误时会抛出的错误。
在进行xml的解析之前,我们先来看看Document的定义
Interface Document extends node
因此包括node的方法我们都是可以使用的,那么实际在解析一个xml文件的时候,我们经常需要进行的操作大致为一下几种,
1.获取当前node的tagname
2.获取当前node的attributes
3.选中当前node下的特定child node
4.获取当前node下的child List<node>
所以我们可以定义一个类来给所有的node添加这种通用的特性
static class Entry { private Element element; private Map<String, String> attributes; private List<Entry> children; private Map<String, List<Entry> > selected = new HashMap<String, List<Entry> >(); public Entry(final Element element) { this.element = element; } public String getName() { return this.element.getTagName(); } public Map<String, String> getAttributes() { if(this.attributes != null) { return this.attributes; } this.attributes = new LinkedHashMap<String, String >(); final NamedNodeMap attrs = this.element.getAttributes(); for(int i = 0; i < attrs.getLength(); ++i ) { final Attr attr = (Attr)attrs.item(i); this.attributes.put(attr.getName(), attr.getValue()); } return this.attributes; } public String getAttribute(final String attributeName) { final Attr attribute = this.element.getAttributeNode(attributeName); if (attribute == null) { return null; } return attribute.getValue(); } public List<Entry> selectChildren(final String tagName) { List<Entry> children = this.selected.get(tagName); if ( children!=null ) { return children; } children = new ArrayList<Entry>(); for( final Entry child : getChildren() ) { if ( child.getName().equals(tagName)) { children.add(child); } } this.selected.put(tagName, children); return children; } public List<Entry> getChildren() { if ( this.children!=null) { return this.children; } this.children = new ArrayList<Entry>(); final NodeList childNodes = this.element.getChildNodes(); for( int i = 0; i<childNodes.getLength(); ++i) { final Node childNode = childNodes.item(i); if ( childNode instanceof Element ) { this.children.add(new Entry((Element)childNode)); } } return this.children; } }
这里我使用的时shimeji里面实现Entry类,然后我们创建一个Reader类来试着读取某个xml的参数
static class Reader { private final Map<String, String> constants = new LinkedHashMap<String, String>(); public void load(final Entry configurationNode) throws IOException { for (final Entry constant : configurationNode.selectChildren("定数")) { System.out.println("constant is: " + constant.getAttribute("値")); constants.put( constant.getAttribute("名前"), constant.getAttribute("値") ); } for (final Entry list : configurationNode.selectChildren("動作リスト")) { for (final Entry node : list.selectChildren("動作")) { System.out.println("node name is: " + node.getName()); } } for (final Entry list : configurationNode.selectChildren("行動リスト")) { System.out.println("list name is: " + list.getName()); loadBehaviors(list, new ArrayList<String>()); } } private void loadBehaviors(final Entry list, final List<String> conditions) { for (final Entry node : list.getChildren()) { if (node.getName().equals("条件")) { final List<String> newConditions = new ArrayList<String>(conditions); newConditions.add(node.getAttribute("条件")); loadBehaviors(node, newConditions); } else if (node.getName().equals("行動")) { System.out.println("action is: " + node.getName()); } } } }
下面附上运行结果
这段代码已经上传到:
https://github.com/AlanJager/java_learning
0 0
- java 解析XML DocumentBuilderFactory
- DocumentBuilderFactory解析xml
- 读取和解析XML数据----读取XML DocumentBuilderFactory
- 采用DocumentBuilderFactory来进行XML文件的解析
- Java调用XML的方法:DocumentBuilderFactory
- Java调用XML的方法:DocumentBuilderFactory
- Java调用XML的方法:DocumentBuilderFactory
- Java调用XML的方法:DocumentBuilderFactory
- Java调用XML的方法:DocumentBuilderFactory
- Java调用XML的法:DocumentBuilderFactory
- Java调用XML的方法之DocumentBuilderFactory
- Java调用XML的方法:DocumentBuilderFactory
- Java调用XML的方法:DocumentBuilderFactory
- Java调用XML的方法:DocumentBuilderFactory
- Java调用XML的方法:DocumentBuilderFactory
- Java调用XML的方法:DocumentBuilderFactory
- Java调用XML的方法:DocumentBuilderFactory
- Java调用XML的方法:DocumentBuilderFactory
- 如何在Fragment中监听触摸事件
- EBS mo_glob_org_access_tmp表的分析
- 基于轮廓线索(Contour Cues)的实时行人检测
- 学习python的第二十一天
- JavaScript 中的 this !
- DocumentBuilderFactory解析xml
- VC 获取本地ip地址
- 多态/抽象/接口
- BAT批处理
- nginx的403 Forbidden解决的办法(权限文件和文件不存在)
- 观察者模式(Observer)
- 【Android】学习ListView遇到的问题
- Android init.rc文件解析过程详解(一)
- 关于获取iOS设备唯一标识