dom4j selectNodes 取不到值 因为XML带有命名空间 HL7

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();
    } 
}

 

posted @ 2022-07-05 19:10  VipSoft  阅读(445)  评论(0编辑  收藏  举报