本文主要是介绍没有闭合(结束)标签XML文件的解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先上XML文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<RESULT_INFO><PATIENT><origin value="XX科技公司" /><id value="1234568" /><name value="李四" /><sex value="男" /><age value="20" /><exmatime value="2018-01-26 18:26:37" /></PATIENT><DATAINFO><samplerate value="500" /><baseline value="32768" /><advalue value="262" /><channels value="9" /><channelbyte value="4" /><analysechannel value="6" /></DATAINFO><EXAM_RESULT><hr value="0" /><p_time value="0" /><pr_interval value="0" /><qrs_time value="0" /><qt_interval value="0" /><qtc_interval value="0" /><p_axis value="0.0" /><qrs_axis value="0.0" /><t_axis value="0.0" /><qrs_t value="0.0" /><sys_conclusion value="心率正常" /><minnsotCode value="157,152,136,139,158,139" /></EXAM_RESULT><print_time value="2018-01-26 18:14:33" />
</RESULT_INFO>
该XML文件与平常所见XML有所不同,我们常见的XML文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<ROOT>
<VERSION>1.0</VERSION>
<USER>5</USER>
<TYPE>SPO2</TYPE>
<SOURCE>IOS</SOURCE>
<content>
<ITEM>
<SPO2>97</SPO2>
<PR>70</PR>
<PI>48</PI>
<TIME>2017-01-06 10:49:10</TIME>
</ITEM>
</content>
</ROOT>
那么非闭合标签的XML文件怎样读取,才能拿到子节点如minnsotCode 的属性值呢,刚开始我使用的是DOM解析,大致是这样,详细代码我就不贴出来了:
DocumentBuilderFactory factory1 = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory1.newDocumentBuilder();Document doc = builder.parse(“demo.xml”);NodeList Item = doc.getElementsByTagName("EXAM_RESULT");for (int i = 0; i < Item.getLength(); i++) {String minnsotCode =doc.getElementsByTagName("minnsotCode ").item(i).getFirstChild().getNodeValue();}
运行后报空指针异常
后使用dom4J解析,终于拿到值。
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader; public static void main(final String[] args) { final SAXParserHandler test = new SAXParserHandler(); try { ECG12 ecg12=test.GetECGData("E:\\test\\20170828.xml");System.out.println(ecg12.getHeartRate());System.out.println(ecg12.getECGResult());System.out.println(ecg12.getMinnsotCode ());} catch (final Exception e) { e.printStackTrace(); } } public static ECG12 getECG12Test(final Element node){ECG12 ecg12=new ECG12();getNodes(node,ecg12);return ecg12;}public ECG12 GetECGData(String path) throws Exception { final SAXReader sax = new SAXReader();final File xmlFile = new File(path); //"E:\\test\\20170828.xml"final Document document = sax.read(xmlFile);final Element root = document.getRootElement();ECG12 ecg12 = getECG12Test(root);return ecg12;} public static void getNodes(final Element node,ECG12 ecg12) { final List<Attribute> listAttr = node.attributes();for (final Attribute attr : listAttr) {if(node.getName().equalsIgnoreCase("hr")){ecg12.setHeartRate(Integer.parseInt(attr.getValue()));} if(node.getName().equalsIgnoreCase("sys_conclusion")){String ecgResult=attr.getValue();ecg12.setECGResult(ecgResult);}else if(node.getName().equalsIgnoreCase("minnsotcode")){String minnsotcode=attr.getValue();ecg12.setECGData(minnsotcode);}} final List<Element> listElement = node.elements(); for (final Element e : listElement){ getNodes(e,ecg12);}} ```以上ECG12为要取值的实体类,最终目的拿到各个属性值赋值,然后在存进数据库,本人才疏学浅,如有不足之处,欢迎指正,同时如果有更好的解决办法,也欢迎提出。
这篇关于没有闭合(结束)标签XML文件的解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!