【问题标题】:how to read complex xml using common configuration java如何使用通用配置java读取复杂的xml
【发布时间】:2012-10-03 07:30:40
【问题描述】:

我需要使用通用配置读取复杂的 XML 文件。有人可以帮我吗?

<configurations>  
<configuration type="application configuration">  
    <group id="1" name="President">  
        <sentiment-searcher>  
            <sentiment id="1" type="sentiment candidate 1">  
                <positive>'Obama okay' 'Obama great'</positive>  
                <negative>'Obama bad' 'Obama idiot'</negative>  
            </sentiment>  
            <sentiment id="2" type="sentiment candidate 2">  
                <positive>'Putin okay'</positive>  
                <negative>'Putin bad'</negative>  
            </sentiment>  
        </sentiment-searcher>  
    </group>  
</configuration>  
</configurations>  

【问题讨论】:

  • 使用 xml 解析器。 DOM 方便,SAX/STAX 用于非常大的文件。
  • 除非您遇到任何特定问题,否则您正在寻找的是使用通用配置 (apache) 的标准。检查this example
  • 是的,这是标准的,对不起先生,我是新手 :)

标签: java xml apache-commons-config


【解决方案1】:

commons configuration 可能不是此任务的正确工具。当您已经知道键名时(即,当您知道要获取 some.important.config 时),配置 API 很有用,但当您不知道元素是什么样子以及它们的作用是什么时,它不太适合处理复杂的嵌套数据结构。键可能是。

STaX API 更适合这项任务。 Here is a tutorial using the woodstox framework.

如果您仍想使用 commons 配置,文档说明如何read the config from a file 和如何access nested values

【讨论】:

    【解决方案2】:

    你可以:

    1. 在 Java 中定义您的类模型(配置、组、情感等)
    2. 使用 Jaxb 转换您的 xml(例如)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-20
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      相关资源
      最近更新 更多