简体   繁体   English

如何使用通用配置Java读取复杂的xml

[英]how to read complex xml using common configuration java

I need to read complex XML files using common configuration. 我需要使用通用配置读取复杂的XML文件。 can somebody help me? 有人可以帮我吗?

<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>  

commons configuration is probably not the right tool for this task. commons配置可能不是此任务的正确工具。 The Configuration API is useful when you already know the key names (ie when you know you want to get some.important.config ) but it's less suited for processing complex, nested data structures when you don't know what the elements look like and what the keys might be. 当您已经知道键名时(例如,当您知道要获取some.important.config ),Configuration API很有用,但是当您不知道元素的外观和名称时,它就不适用于处理复杂的嵌套数据结构。关键是什么。

The STaX API is much better suited for this task. STaX API更适合此任务。 Here is a tutorial using the woodstox framework . 这是使用woodstox框架的教程

If you still want to use commons configuration, the documentation explains how to read the config from a file and how to access nested values . 如果仍要使用commons配置,则文档说明如何从文件读取配置以及如何访问嵌套值

You can: 您可以:

  1. Define your class model in java (Configuration, Group, Sentiment, etc.) 在Java中定义类模型(配置,组,情感等)
  2. Convert your xml using Jaxb (for example) 使用Jaxb转换xml(例如)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM