首页 > 解决方案 > 获取 XML 注释值

问题描述

我有这个 XML 文件:

<!-- 
  @author: test
  @ticket: 433
-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans                            http://www.springframework.org/schema/beans/spring-beans.xsd">

我想获取@author 和@ticket 的值。我试过这个:

XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream("C:\\rootApplicationContext.xml"));
        while (xr.hasNext()) {
            if (xr.next() == XMLStreamConstants.COMMENT) {
                String comment = xr.getText();

                System.out.println("!!!!! comment " + comment);
            } }

但我将整个评论作为一个字符串。有没有办法获取键值对?

标签: java

解决方案


推荐阅读