parse xml using dom java

前端 未结 3 1970
青春惊慌失措
青春惊慌失措 2021-01-25 20:15

I have the bellow xml:


    
        
            wish
             


        
3条回答
  •  遥遥无期
    2021-01-25 20:37

    I like to use XMLBeam for such tasks:

    public class Answer {
    
        @XBDocURL("resource://data.xml")
        public interface DataProjection {
    
            public interface Topic {
                @XBRead("./@id")
                int getID();
    
                @XBRead("./@percentage")
                String getPercentage();
            }
    
            @XBRead("/modelingOutput/listOfDocs//document/topic")
            List getTopics();
        }
    
        public static void main(final String[] args) throws IOException {
            final DataProjection dataProjection = new XBProjector().io().fromURLAnnotation(DataProjection.class);
            for (Topic topic : dataProjection.getTopics()) {
                System.out.println(topic.getID() + ": " + topic.getPercentage());
            }
        }
    }
    

    There is even a convenient way to convert the percentage to float or double. Tell me if you like to have an example.

提交回复
热议问题