使用 Java 解析 XML 中的特定元素

发布于 2024-11-28 19:47:20 字数 1677 浏览 0 评论 0原文

我收到以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还给你自由 2024-12-05 19:47:20

Java XPath API 使用起来很简单:

String xmlData = "<test><one><URL128 myAttribute='value' /></one></test>";
InputSource source = new InputSource(new StringReader(xmlData)); //or use your own input source

XPath xPath = XPathFactory.newInstance().newXPath();

NodeList list = (NodeList)xPath.evaluate("//URL128", source, XPathConstants.NODESET);
List<Element> elements = new ArrayList<Element>(list.getLength());
for (int i = 0; i < list.getLength(); i++)
{
    elements.add((Element)list.item(i));
}

The Java XPath API is simple to use:

String xmlData = "<test><one><URL128 myAttribute='value' /></one></test>";
InputSource source = new InputSource(new StringReader(xmlData)); //or use your own input source

XPath xPath = XPathFactory.newInstance().newXPath();

NodeList list = (NodeList)xPath.evaluate("//URL128", source, XPathConstants.NODESET);
List<Element> elements = new ArrayList<Element>(list.getLength());
for (int i = 0; i < list.getLength(); i++)
{
    elements.add((Element)list.item(i));
}
把回忆走一遍 2024-12-05 19:47:20

使用 SAX,并实现 startElement 方法,这样如果元素名称是“URL128”,则提取三个属性 ScopeTypeValue,将它们存储在自定义的对象,并将该对象添加到列表

这将既简单又快捷。

Use SAX, and implement the startElement method so that if the element name is "URL128", you extract the three attributes Scope, Type and Value, store them in a custom object, and add this object to a List.

It will be both easy and fast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文