1

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>
3
  • if thats all the xml then SAX would okay but I always ask people to look at groovy xmlslurper Commented Aug 10, 2011 at 7:11
  • 1
    By 'efficient', do you mean fast, or do you mean least effort/code? If fast, I might recommend SAX or StAX parsing (or even custom parsing just to get at that one element). If least effort, then DOM parsing or even a 3rd-party lib that provides XPath evaluation. Commented Aug 10, 2011 at 7:13
  • least effort in coding without majorly sacrificing performance Commented Aug 10, 2011 at 8:22

2 Answers 2

4

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));
}
Sign up to request clarification or add additional context in comments.

1 Comment

great example prunge! are you using javax.xml.bind.Element in your example? I wasn't sure how to extract the date from the Collection of Element objects. I probably have the wrong import...
2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.