xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<POOR_IN200901UV ITSVersion="XML_1.0" xmlns="urn:hl7-org:v3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:hl7-org:v3 ../../Schemas/POOR_IN200901UV20.xsd">
<id extension="BS002" />
<creationTime value="20120106110000" />
<interactionId root="2.16.840.1.113883.1.6" extension="POOR_IN200901UV20" />
<processingCode code="P" />
<!-- 消息处理模式: A(Archive); I(Initial load); R(Restore from archive); T(Current
processing) -->
<processingModeCode code="T" />
<!-- 消息应答: AL(Always); ER(Error/reject only); NE(Never) -->
<acceptAckCode code="NE" /> <!-- 接受者 -->
<receiver typeCode="RCV">
<device classCode="DEV" determinerCode="INSTANCE">
<!-- 接受者ID -->
<id>
<item root="1.2.156.456150488.1.1.19" extension=""/>
</id>
</device>
</receiver>
<!-- 发送者 -->
<sender typeCode="SND">
<device classCode="DEV" determinerCode="INSTANCE">
<!-- 发送者ID -->
<id>
<item root="1.2.156.456150488.1.1.19" extension="S002"/>
</id>
</device>
</sender>
<controlActProcess classCode="CACT" moodCode="EVN">
<!-- 消息交互类型 @code: 新增 :new 删除:delete 补发:replace-->
<code code="new"></code>
<subject typeCode="SUBJ" xsi:nil="false">
<placerGroup classCode="GROUPER" moodCode="RQO">
<subject typeCode="SBJ">
<patient classCode="PAT">
<id>
<!-- 域ID -->
<item root="1.2.156.456150488.1.2.1.2" extension="01" />
<!-- 患者ID -->
<item root="1.2.156.456150488.1.2.1.3" extension="09102312" />
<!-- 就诊号 -->
<item root="1.2.156.456150488.1.2.1.12" extension="0910238" />
</id>
<!-- 病区编码/病区名 床号 -->
<addr xsi:type="BAG_AD">
<item use="TMP">
<part type="BNR" value="9A血液科" code="09808" codeSystem="1.2.156.456150488.1.1.33"/>
<part type="CAR" value="06" />
</item>
</addr>
</patient>
</subject>
</controlActProcess>
</POOR_IN200901UV>

三种取值方法,命名空间:xmlns="urn:hl7-org:v3"

/**
* 推荐使用
* @throws Exception
*/
@Test
void hl7V3Parse1() throws Exception { String xmlPath = "D:\\BS002.xml";
File xmlFile = new File(xmlPath);
SAXReader reader = new SAXReader();
Document doc = reader.read(xmlFile); //添加命名空间
Map<String, String> xmlMap = new HashMap<>();
xmlMap.put("s", "urn:hl7-org:v3"); //作用域在文档上,方便多次 select Node
reader.getDocumentFactory().setXPathNamespaceURIs(xmlMap); Node interactionId = doc.selectSingleNode("s:POOR_IN200901UV/s:creationTime/@value");
System.out.println(interactionId.getText()); //当有多个item 时,指定 root = 1.2.156.456150488.1.2.1.3 的 extension 值
Node patientLidNode = doc.selectSingleNode("/s:POOR_IN200901UV/s:controlActProcess/s:subject/s:placerGroup/s:subject/s:patient/s:id/s:item[@root='1.2.156.456150488.1.2.1.3']/@extension");
System.out.println(patientLidNode.getText());
} /**
* 不太方便
* @throws Exception
*/
@Test
void hl7V3Parse2() throws Exception { String xmlPath = "D:\\BS002.xml";
File xmlFile = new File(xmlPath);
SAXReader reader = new SAXReader();
Document doc = reader.read(xmlFile); //添加命名空间
Map<String, String> xmlMap = new HashMap<>();
xmlMap.put("s", "urn:hl7-org:v3");
//作用域在 XPath 上
XPath xPath = doc.createXPath("s:POOR_IN200901UV/s:creationTime/@value");
xPath.setNamespaceURIs(xmlMap);
Node name = xPath.selectSingleNode(doc);
System.out.println(name.getText());
} /**
* HL7 节点太多,这种方法相当麻烦
* @throws Exception
*/
@Test
void hl7V3Parse3() throws Exception {
String xmlPath = "D:\\BS002.xml";
File xmlFile = new File(xmlPath);
SAXReader reader = new SAXReader();
Document doc = reader.read(xmlFile); Attribute name = doc.getRootElement().element("creationTime").attribute("value");
System.out.println(name.getValue());
}

赋值,保存 HL7 XML

void hl7ParseBS004() throws Exception {
String xmlPath = "D:\\BS004.xml";
String savePath = "D:\\BS004_save.xml";
File xmlFile = new File(xmlPath);
SAXReader reader = new SAXReader();
Document doc = reader.read(xmlFile); //添加命名空间
Map<String, String> xmlMap = new HashMap<>();
xmlMap.put("s", "urn:hl7-org:v3"); //作用域在文档上,方便多次 select Node
reader.getDocumentFactory().setXPathNamespaceURIs(xmlMap); //消息创建时间
Node creationTimeNode = doc.selectSingleNode("/s:POOR_IN200901UV/s:creationTime/@value");
creationTimeNode.setText(DateUtil.format(new Date(), "yyyyMMddHHmmss"));
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
outputFormat.setEncoding("UTF-8");
XMLWriter writer = null;
try {
writer = new XMLWriter(new FileWriter(savePath), outputFormat);
writer.write(doc);
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}

dom4j selectNodes 取不到值 因为XML带有命名空间 HL7的更多相关文章

  1. @Value取不到值引出的spring的2种配置文件applicationContext.xml和xxx-servlet.xml

    项目中经常会用到配置文件,定义成properties的形式比较常见,为了方便使用一般在spring配置文件中做如下配置: <context:property-placeholder ignore ...

  2. org.apache.commons.lang3.tuple.Pair 作为更新参数,XML 中的 Sql 取不到值、报错

    项目用的 Mybatis,今天改一个需求,落地实现是批量更新,且只需要根据主键(id)来更新一个字段(name). 于是,没有犹豫,像下面这样设计了数据结构: 既然是批量更新,那外层肯定是 List ...

  3. spring注解@Value取不到值【转】

    spring注解@Value取不到值 今天在一个项目中发现一个情况,在Service中取不到name值,直接输出了{name}字符串,找了好久,最后在一篇文章中找到解决方案. 解决这个问题的一篇文章( ...

  4. 数据库TINYINT类型 参数0 mybatis取不到值

    tinyint存储0的奇怪问题  数据库TINYINT类型   参数0  mybatis取不到值 postman 传参 audited   =0          audited   =1  两种情况 ...

  5. 控制层@Value注解取不到值

    @Value("${enable-upload-image}") private String enable; 如上所示,同样的代码,写在在业务层,运行时能取到正确的值,但在控制层 ...

  6. [转载]ASP.NET中TextBox控件设立ReadOnly="true"后台取不到值

    原文地址:http://www.cnblogs.com/yxyht/archive/2013/03/02/2939883.html ASP.NET中TextBox控件设置ReadOnly=" ...

  7. IE10中session失效取不到值的问题

    在eworkflow工作流,ebiao报表,eform自定义表单产品升级到IE10的时候,系统登录后,总是会取不到session中的值. for j2ee版本和for dotnet版本都一样取不到值. ...

  8. struts2下s:iterator取不出值

    1:问题起因:通过action方法连接数据库取出Arraylist集合,在前台页面遍历显示无数据,用s:debug查看 stack里有值,用了各种方法,包括session传值,但是仍然取不出来. 2: ...

  9. [转]用Linq取CheckBoxList選取項目的值

    本文转自:http://www.dotblogs.com.tw/hatelove/archive/2011/11/17/linq-checkboxlist-items-selected-values. ...

  10. Selenium2学习-028-WebUI自动化实战实例-026-获取页面元素值或者元素属性值

    在自动化脚本编写过程中,经常需要获取页面元素的文本进行判断,以便对于不同的文本进行不同的处理.比如:很多的购物网站,加入购物车的按钮是有多个状态的(加入购物车.到货通知.暂不销售等),那么在实际的操作 ...

随机推荐

  1. [Python急救站课程]简单的人机对话

    一个简单的人机对话程序 name = input("输入姓名:") # input输入数据 print("{}同学,学好Python,前途无量!".format ...

  2. STL deque容器

    deque - 双向队列 1.队列的基本知识 队列的基本特性就是先进先出(FIFO),也就是第一个进去的元素第一个出来.即队列就是一个只允许在一端进行插入,在另一端进行删除操作的线性表.Queue接口 ...

  3. 轻松应对复杂集成场景!用友U8API开发适配

    在企业上云的大趋势下,U8+ 全面转向互联网方向,深入融合云应用,一站式提供财务.营销.制造.采购.设计.协同.人力等领域的"端 + 云"服务,并通过软硬一体化.产业链协同的策略全 ...

  4. CSS+HTML初学跟踪项目记录笔记【防丢失】(文章发布系统)二【鸽了】

    贴上源代码 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...

  5. Salesforce LWC学习(四十六) 自定义Datatable实现cell onclick功能

    本篇参考:https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable 背景:我们有时会有这种类 ...

  6. mysql的用户管理和权限控制

    1.创建用户用 create user '用户名' @ 'localhost' identified by '密码'; 这串代码是创建当地用,是这台电脑的用户,因为有个localhost: creat ...

  7. MyBatisPlus简介

    MyBatisPlus特性 国内的一个网站 网站地址简介 | MyBatis-Plus (baomidou.com)

  8. MySQL部署后配置

    授权root用户登录 #仅本地登录,修改密码用 alter user root@'localhost' identified with mysql_native_password by'******* ...

  9. 交换机SNMP配置

    配置参考v2c为例 1.华为 snmp-agent protocol source-interface vlanif 1 ##S573x以上型号交换机需要snmp-agentsnmp-agent sy ...

  10. 新版的Django中的path不能使用正则表达式

    新版的path 虽然 取代了 之前的url,但是在写路由的时候不能在路由中直接写正则表达式,不然会找不到页面. 解决方法使用 re_path from django.urls import re_pa ...