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

JavaJava EE

Avatar of undefined
Last Comment
cgray1223

8/22/2022 - Mon
sweetfa2

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

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
CEHJ

XPath is good. The expression you need is

//URL128[@Value]
Your help has saved me hundreds of hours of internet surfing.
fblack61
cgray1223

ASKER
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
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
cgray1223

ASKER
 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.
CEHJ

Actually
for (int i = 0; i < list.getLength(); i++) {

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

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
cgray1223

ASKER
What package is the Element object from?