Java
SimpleXML库看起来主要是用于将Java对象序列化为XML。它确实支持反序列化(将XML解析为Java)-但文档中说:
org.simpleframework.xml.stream-
在一组第三方库上提供XML解析层。流包提供了一个基于事件的XML解析框架,该框架可与多个第三方库(例如DOM,StAX和XML pull
API)一起使用。
换句话说,它委托给其他程序包。
因此,这是一种StAX方法。它使用基于指针的StAX
XML流读取(而不是替代的StAX事件阅读器),因为它说,这里的“比较光标和迭代器的API”部分:
如果要针对特别受内存限制的环境(例如Java ME)进行编程,则可以使用游标API编写更小巧,更有效的代码。
这是方法。它只是解析输入文件并打印出相关部分。它仅尝试演示如何访问所需的数据项,而不会尝试做更多的事情:
import java.io.FileInputStream;import java.io.FileNotFoundException;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;import javax.xml.stream.events.XMLEvent;...private final String fileNameString = "C:/tmp/files/graphml.xml";public void read() throws XMLStreamException, FileNotFoundException { XMLInputFactory xmlif = XMLInputFactory.newInstance(); XMLStreamReader xmlr = xmlif.createXMLStreamReader(fileNameString, new FileInputStream(fileNameString)); while (xmlr.hasNext()) { int eventType = xmlr.next(); if (eventType == XMLEvent.START_ELEMENT) { if (xmlr.getLocalName().equals("node")) { if (xmlr.getAttributeCount() == 1) { // a safety check String nodeID = xmlr.getAttributevalue(0); System.out.println("---"); System.out.println("Node ID: " + nodeID); } } if (xmlr.getLocalName().equals("data")) { if (xmlr.getAttributeCount() == 1) { // a safety check String dataKey = xmlr.getAttributevalue(0); String dataValue = xmlr.getElementText(); System.out.println("Data key: " + dataKey + " Data value: " + dataValue); } } } }}
关键部分是
XMLStreamReader对象,它使您可以轻松访问每个标签的文本内容以及属性。
根据SimpleXML规范,应该可以从SimpleXML中使用它-但我没有尝试过。或者,也许您可以只使用完全没有SimpleXML的StAX。
就我而言,这将输出以下输出:
Node ID: L08-022Data key: lat Data value: 40.69330963Data key: lon Data value: -73.98752537Data key: name Data value: Data key: waypoint_type Data value: escalatorData key: level_id Data value: 1080000Data key: on_starting_route Data value: Data key: on_finish_route Data value: ---Node ID: L08-023Data key: lat Data value: 40.69318355Data key: lon Data value: -73.98755793Data key: name Data value: Data key: waypoint_type Data value: stairsData key: level_id Data value: 1080000Data key: on_starting_route Data value: Data key: on_finish_route Data value: ---Node ID: L08-024Data key: lat Data value: 40.69316844Data key: lon Data value: -73.98755873Data key: name Data value: Data key: waypoint_type Data value: stairsData key: level_id Data value: 1080000Data key: on_starting_route Data value: Data key: on_finish_route Data value:
我使用的示例文件是这样的:
<?xml version='1.0' encoding='utf-8'?><graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"><key attr.name="weight" attr.type="double" for="edge" id="weight" /> <key attr.name="edgeid" attr.type="string" for="edge" id="edgeid" /> <key attr.name="alpha" attr.type="string" for="edge" id="alpha" /> <key attr.name="intendedpathlonlat" attr.type="string" for="edge" id="intendedpathlonlat" /> <key attr.name="levelid" attr.type="string" for="edge" id="levelid" /> <key attr.name="weight" attr.type="long" for="edge" id="weight" /> <key attr.name="type" attr.type="string" for="edge" id="type" /> <key attr.name="relatedroutes" attr.type="string" for="node" id="relatedroutes" /> <key attr.name="description" attr.type="string" for="node" id="description" /> <key attr.name="title" attr.type="string" for="node" id="title" /> <key attr.name="on_finish_route" attr.type="string" for="node" id="on_finish_route" /> <key attr.name="on_starting_route" attr.type="string" for="node" id="on_starting_route" /> <key attr.name="level_id" attr.type="string" for="node" id="level_id" /> <key attr.name="waypoint_type" attr.type="string" for="node" id="waypoint_type" /> <key attr.name="name" attr.type="string" for="node" id="name" /> <key attr.name="lon" attr.type="string" for="node" id="lon" /> <key attr.name="lat" attr.type="string" for="node" id="lat" /> <graph edgedefault="directed" id="new id here"> <node id="L08-022"> <data key="lat">40.69330963</data> <data key="lon">-73.98752537</data> <data key="name" /> <data key="waypoint_type">escalator</data> <data key="level_id">1080000</data> <data key="on_starting_route" /> <data key="on_finish_route" /> </node> <node id="L08-023"> <data key="lat">40.69318355</data> <data key="lon">-73.98755793</data> <data key="name" /> <data key="waypoint_type">stairs</data> <data key="level_id">1080000</data> <data key="on_starting_route" /> <data key="on_finish_route" /> </node> <node id="L08-024"> <data key="lat">40.69316844</data> <data key="lon">-73.98755873</data> <data key="name" /> <data key="waypoint_type">stairs</data> <data key="level_id">1080000</data> <data key="on_starting_route" /> <data key="on_finish_route" /> </node> </graph></graphml>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)