使用 Java 解析 XML 中的特定元素
我收到以下 xml 响应,我需要进行解析,以便我得到的只是 URL128 xml 元素值的集合。关于用 Java 实现这一点的最有效方法有什么想法吗?
<?xml version="1.0" encoding="utf-8"?>
<imagesXML>
<Images>
<Image>
<ImageUID Scope="Public" Type="Guid" Value="{7f2535d0-9a41-4997-9694-0a4de569e6d9}"/>
<CorbisID Scope="Public" Type="String" Value="42-15534232"/>
<Title Scope="Public" Type="String" Value="Animal"/>
<CreditLine Scope="Public" Type="String" Value="© Robert Llewellyn/Corbis"/>
<IsRoyaltyFree Scope="Public" Type="Boolean" Value="False"/><AspectRatio Scope="Public" Type="String" Value="1.500000"/>
<URL128 Scope="Public" Type="String" Value="http://cachens.corbis.com/CorbisImage/thumb/15/53/42/15534232/42-15534232.jpg"/>
</Image>
<Image>
<ImageUID Scope="Public" Type="Guid" Value="{7f2535d0-9a41-4997-9694-0a4de569e6d9}"/>
<CorbisID Scope="Public" Type="String" Value="42-15534232"/>
<Title Scope="Public" Type="String" Value="Animal"/>
<CreditLine Scope="Public" Type="String" Value="© Robert Llewellyn/Corbis"/>
<IsRoyaltyFree Scope="Public" Type="Boolean" Value="False"/><AspectRatio Scope="Public" Type="String" Value="1.500000"/>
<URL128 Scope="Public" Type="String" Value="http://cachens.corbis.com/CorbisImage/thumb/15/53/42/15534232/42-15534232.jpg"/>
</Image>
</Images>
</imagesXML>
I'm receiving the below xml response and I need to parse so that all I get is Collection of the URL128 xml Element values. Any ideas on the most efficient way to accomplish this in Java?
<?xml version="1.0" encoding="utf-8"?>
<imagesXML>
<Images>
<Image>
<ImageUID Scope="Public" Type="Guid" Value="{7f2535d0-9a41-4997-9694-0a4de569e6d9}"/>
<CorbisID Scope="Public" Type="String" Value="42-15534232"/>
<Title Scope="Public" Type="String" Value="Animal"/>
<CreditLine Scope="Public" Type="String" Value="© Robert Llewellyn/Corbis"/>
<IsRoyaltyFree Scope="Public" Type="Boolean" Value="False"/><AspectRatio Scope="Public" Type="String" Value="1.500000"/>
<URL128 Scope="Public" Type="String" Value="http://cachens.corbis.com/CorbisImage/thumb/15/53/42/15534232/42-15534232.jpg"/>
</Image>
<Image>
<ImageUID Scope="Public" Type="Guid" Value="{7f2535d0-9a41-4997-9694-0a4de569e6d9}"/>
<CorbisID Scope="Public" Type="String" Value="42-15534232"/>
<Title Scope="Public" Type="String" Value="Animal"/>
<CreditLine Scope="Public" Type="String" Value="© Robert Llewellyn/Corbis"/>
<IsRoyaltyFree Scope="Public" Type="Boolean" Value="False"/><AspectRatio Scope="Public" Type="String" Value="1.500000"/>
<URL128 Scope="Public" Type="String" Value="http://cachens.corbis.com/CorbisImage/thumb/15/53/42/15534232/42-15534232.jpg"/>
</Image>
</Images>
</imagesXML>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java XPath API 使用起来很简单:
The Java XPath API is simple to use:
使用 SAX,并实现
startElement
方法,这样如果元素名称是“URL128”,则提取三个属性Scope
、Type
和Value
,将它们存储在自定义的对象,并将该对象添加到列表
。这将既简单又快捷。
Use SAX, and implement the
startElement
method so that if the element name is "URL128", you extract the three attributesScope
,Type
andValue
, store them in a custom object, and add this object to aList
.It will be both easy and fast.