Link to home
Start Free TrialLog in
Avatar of cgray1223
cgray1223

asked on

Use Java to Parse XML for Specific Element

Hello,

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?

 

     
 <?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>

Open in new window

Avatar of sweetfa2
sweetfa2
Flag of Australia image

Efficient is relative - efficient in memory usage or cpu cycles or lines of code :)

You could use the castor library How to use Castor to create java pojos of your representation.

Another alternative is using the JAXB library Using JAXB
Avatar of cgray1223
cgray1223

ASKER

Here is the full xml i'm trying to parse.  I thought xpath may be the right choice, thoughts?

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><wsu:Timestamp xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility"><wsu:Created>2011-08-10T08:41:35Z</wsu:Created><wsu:Expires>2011-08-10T08:46:35Z</wsu:Expires></wsu:Timestamp></soap:Header><soap:Body><SearchResponse xmlns="http://c1.net.corbis.com/"><searchResultDataXML><SearchResultData><SearchRequestUID Scope="Public" Type="Guid" Value="{20bd500c-1b71-4d9c-a774-7ceaed30fece}"/><StartPosition Scope="Public" Type="Long" Value="1"/><EndPosition Scope="Public" Type="Long" Value="50"/><TotalHits Scope="Public" Type="Long" Value="323515"/></SearchResultData></searchResultDataXML><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="{4ca55d21-096a-4e08-bb11-73cd57210712}"/><CorbisID Scope="Public" Type="String" Value="00399010018"/><Title Scope="Public" Type="String" Value="Animal"/><CreditLine Scope="Public" Type="String" Value="© EL KURIKI/amanaimages/Corbis"/><IsRoyaltyFree Scope="Public" Type="Boolean" Value="False"/><AspectRatio Scope="Public" Type="String" Value="1.305184"/><URL128 Scope="Public" Type="String" Value="http://cachens.corbis.com/CorbisImage/thumb/14/62/78/14627855/00399010018.jpg"/></Image><Image><ImageUID Scope="Public" Type="Guid" Value="{728d9c80-f7d9-4c65-ae3c-87feddca4b69}"/><CorbisID Scope="Public" Type="String" Value="42-20093017"/><Title Scope="Public" Type="String" Value="animals"/><CreditLine Scope="Public" Type="String" Value="© Illustration Works/Corbis"/><IsRoyaltyFree Scope="Public" Type="Boolean" Value="False"/><AspectRatio Scope="Public" Type="String" Value="1.333333"/><URL128 Scope="Public" Type="String" Value="http://cachens.corbis.com/CorbisImage/thumb/20/09/30/20093017/42-20093017.jpg"/></Image></Images></imagesXML></SearchResponse></soap:Body></soap:Envelope>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sweetfa2
sweetfa2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
XPath is good. The expression you need is

//URL128[@Value]
CEHJ,

I tried your example path but I'm unable to get it to select the right xml element's value.  I wrote a sample with a scaled down version of the xml.  Any ideas where I'm going wrong?  My output from my sys out is value=[URL128: null]


package javaapplication2;
import java.io.StringReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 *
 * @author CGray
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args){
        String content = "<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></Images></imagesXML>";
        InputSource source = new InputSource(new StringReader(content));
        XPath xPath = XPathFactory.newInstance().newXPath();
        NodeList list=null;
        try{
            list = (NodeList)xPath.evaluate("//URL128[@Value]", source, XPathConstants.NODESET);
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
        }     
        for (int i = 0; i < list.getLength(); i++){
            System.out.println("value=" + list.item(i));
        }
    }

}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
 list = (NodeList) xPath.evaluate("//URL128/@Value", source, XPathConstants.NODESET); worked for me but not //URL128[@Value].  The list.item(i).getTextContent() did get the text though thanks, both very helpful.
Actually
for (int i = 0; i < list.getLength(); i++) {

            System.out.println(((Element)list.item(i)).getAttribute("Value"));
        }

Open in new window

What package is the Element object from?