将XML解析为Java对象
我试图确定将从webservice调用得到的XML响应解析为Java对象的最佳方法。 使用JAXB似乎是最简单的方法,但我得到的每个示例都要求您有一个模板Java类,它将是XML转换成的Java类型。 我的xml如下所示
<?xml version="1.0" encoding="utf-8" ?>
<entry_list version="1.0">
<entry id="main[1]"> <hw highlight="yes" hindex="1">main</hw> <sound><wav>main0001.wav</wav></sound> <pr>ˈmeɪn</pr> <fl>adjective</fl> <lb>always used before a noun</lb> <def><dt>:most important :<sx>chief</sx> <sx>principal</sx> <vi>the <it>main</it> idea/point</vi> <vi>the <it>main</it> goal/purpose</vi> <vi>Speed is the <it>main</it> advantage of this approach.</vi> <vi>The company's <it>main</it> office is located in New York.</vi> <vi>the novel's <it>main</it> character</vi> <vi>driving down the <it>main</it> road/highway</vi> <vi>the <it>main</it> gate/entrance</vi> <vi>This dish can be served as a <phrase>main course</phrase> or appetizer.</vi> <vi>And now for the <phrase>main event</phrase> of the evening!</vi></dt></def> <uro><ure>main*ly</ure> <fl>adverb</fl> <utxt><vi>The reviews have been <it>mainly</it> [=<it>mostly</it>] positive.</vi> <vi>a plant found <it>mainly</it> [=<it>chiefly</it>] in coastal regions</vi> <vi>I don't like the plan, <it>mainly</it> because I think it's too expensive.</vi> <vi>The problems have been <it>mainly</it> minor ones. [=most of the problems have been minor ones]</vi> <vi>They depend <it>mainly</it> on/upon fish for food.</vi></utxt></uro></entry>
<entry id="main[2]"> <hw hindex="2">main</hw> <altpr>ˈmeɪn</altpr> <fl>noun</fl> <in><il>plural</il> <if>mains</if></in> <def><sn>1</sn> <sgram>count</sgram> <dt>:the largest pipe in a system of connected pipes <vi>a gas <it>main</it></vi> <vi>a water <it>main</it></vi></dt> <sn>2</sn> <bnote>the mains</bnote> <ssl>Brit</ssl> <sn>a</sn> <dt>:the system of pipes or wires for electricity, gas, or water <vi>My radio runs either off batteries or off <it>the mains</it>.</vi> <un>often used as <it>mains</it> before another noun <vi>We haven't had any <it>mains</it> water/electricity since the storm.</vi></un></dt> <sn>b</sn> <dt>:the place where electricity, gas, or water enters a building or room <vi>Turn off the water at <it>the mains</it>.</vi></dt></def> <dro><dre>in the main</dre> <def><dt>:in general <un>used to say that a statement is true in most cases or at most times <vi>The workers are <it>in the main</it> very capable. [=most of the workers are very capable]</vi> <vi>The weather has <it>in the main</it> been quite good. [=has been quite good most of the time]</vi></un></dt></def></dro></entry>
<entry id="main clause"> <hw>main clause</hw> <fl>noun</fl> <in><il>plural</il> <if>⁓ clauses</if></in> <def><gram>count</gram> <sl>grammar</sl> <dt>:a clause that could be used by itself as a simple sentence but that is part of a larger sentence <ca>called also <cat>independent clause</cat></ca> <dx>compare <dxt>coordinate clause</dxt> <dxt>subordinate clause</dxt></dx></dt></def></entry>
<entry id="main drag"> <hw>main drag</hw> <fl>noun</fl> <in><il>plural</il> <if>⁓ drags</if></in> <def><gram>count</gram> <sl>US</sl> <sl>informal</sl> <dt>:the main street in a town or city <vi>A carload of teenagers were cruising down the <it>main drag</it>.</vi></dt></def></entry>
<entry id="main line"> <hw>main line</hw> <fl>noun</fl> <in><il>plural</il> <if>⁓ lines</if></in> <def><gram>count</gram> <dt>:an important highway or railroad line</dt></def></entry>
<entry id="main man"> <hw>main man</hw> <fl>noun</fl> <in><il>plural</il> <if>⁓ men</if></in> <def><gram>count</gram> <sl>US</sl> <sl>informal</sl> <sn>1</sn> <dt>:someone's best male friend <vi>He's still her <it>main man</it>.</vi></dt> <sn>2</sn> <dt>:the most important or admired man in a group <vi>The team has many good players, but he is clearly the <it>main man</it>.</vi></dt></def></entry>
<entry id="main squeeze"> <hw>main squeeze</hw> <fl>noun</fl> <in><il>plural</il> <if>⁓ squeezes</if></in> <def><gram>count</gram> <sl>chiefly US slang</sl> <dt>:someone's main girlfriend, boyfriend, or lover <vi>She's my <it>main squeeze</it>.</vi></dt></def></entry>
<entry id="main street"> <hw>main street</hw> <fl>noun</fl> <in><il>plural</il> <if>⁓ streets</if></in> <def><sn>1</sn> <sgram>count</sgram> <dt>:the most important street of a U.S. town where there are many stores, banks, etc. <un>often used as a name <vi>The restaurant is at 257 <it>Main Street</it>.</vi></un></dt> <sn>2</sn> <bnote>Main Street</bnote> <sgram>noncount</sgram> <ssl>US</ssl> <dt><un>used to refer to middle-class people in the U.S. who have traditional beliefs and values <vi>What does <it>Main Street</it> think of this policy?</vi></un></dt></def></entry>
<entry id="water main"> <hw>water main</hw> <fl>noun</fl> <in><il>plural</il> <if>⁓ mains</if></in> <def><gram>count</gram> <dt>:a large underground pipe that carries water <vi>The <it>water main</it> burst/broke and flooded the street.</vi></dt></def></entry>
</entry_list>
我的问题是,我是否必须定义要转换成的Java对象?在这种情况下,我担心的是,如果从XML响应中添加或删除当前存在的数据,会发生什么情况。
我也尝试过将XML加载到DOM中,并以这种方式遍历它,但我想知道如果添加或删除元素会发生什么
我只想要某些子节点,如果它们的父节点是某个值,那么任何关于最简单方法的指针都是受欢迎的
# 1 楼答案
使用XML最简单的方法是将其序列化为对象
您可以使用JAXB来实现这一点,这里有一个教程:mykong
只需定义对象的外观即可
下面是一个例子:
得到子元素的每个元素都必须是一个类,如果它重复多次,比如entry或vi,那么它应该是一个列表
# 2 楼答案
我不认为JAXB是这里最好的解决方案。。。最好的解决方案是基于XPath,它允许您在不牺牲代码可维护性的情况下简化编码。。。正如您在下面的代码中所看到的,您的导航只是一个XPATH表达式,整个程序大约有10多行代码,使用XPATH和VTD-XML,顺便说一句,您上面发布的XML示例格式不好
# 3 楼答案
经常被称为POJO,是的,拥有一个POJO是个好主意(甚至可能是必要的)。它定义了数据应该如何表示为对象。如果数据丢失,Java对象的字段将为空。因此,您应该将Java对象定义为所有可能属性的最大覆盖范围
可能会有一些库将附加属性放入Hashmap(至少我知道Jackson可以为JSON做这件事,但不确定XML)
唯一的替代方法是自己手动解析,这样可以保证捕获所有元素,比如节点的深度优先遍历
# 4 楼答案
根据我的经验,当您必须处理非常复杂的XML文档时,可能更容易:
也就是说,假设您有一个非常复杂的XML:
第一步:使用XSLT简化它
第二步。将自己的SimpleForm XML封送到java对象中
这样一来,外部模式和内部逻辑之间就失去了耦合
# 5 楼答案
你可以使用SAX解析器。这种方法的美妙之处在于,除了速度快、内存低之外,你可以忽略所有你不想要或不需要的东西——然后你就不在乎这些部分是否发生了变化。你只要抓住你想要的标签就行了
例如,如果您只对“main子句”标记感兴趣,那么您的处理程序看起来会像: