Results 1 to 4 of 4
  1. #1
    garbar is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Default Checking for XML Attribute Values

    Here is a Snippet from an XML File I have:

    Java Code:
    <guide>
    <r>
    <a n="CategoryPath" v="Home, T Shirts"/>
    <a n="Brand" v="Our Brand"/>
    <a n="LongDescription" v="Mens; Short Sleeve Tee"/>
    <a n="Description" v="Random T Shirt Name"/>
    <a n="Price" v="29"/>
    <a n="bnGender" v="Men's"/>
    <a n="PageType" v="product"/>
    <a n="bnCategory" v="Men's - T Shirts"/>
    <a n="Title" v="XYZ-Blah-Blah"/>
    <a n="Size" v="XXL"/>
    <a n="thumbnailURL" v="/blah.jpg"/>
    </r>
    <r>
    <a n="CategoryPath" v="Home, Pants"/>
    <a n="Brand" v="Our Brand"/>
    <a n="LongDescription" v="Mens; Pants"/>
    <a n="Description" v="Random Pants Name"/>
    <a n="Price" v="29"/>
    <a n="bnGender" v="Women's"/>
    <a n="PageType" v="product"/>
    <a n="bnCategory" v="Women's - Pants"/>
    <a n="Title" v="XYZ-Blah"/>
    <a n="Size" v="XXL"/>
    <a n="thumbnailURL" v="/blah2.jpg"/>
    </r>
    </guide>
    My goal is to query the "a" tag. For example, "If n = Title, return XYZ-Blah-Blah"

    How can I search for where that n tag = "Title" and then return the corresponding v tag value?

    Thanks :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,252
    Blog Entries
    1
    Rep Power
    24

  3. #3
    garbar is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Default

    So far I have tried the following code:

    Java Code:
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    	        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    	        
    	        URL url = new URL(strRecsFeedUrl);
    	        InputStream stream = url.openStream();
    	        Document doc = docBuilder.parse(stream);
    			
    	        /*
    	        doc.getDocumentElement ().normalize ();
    	        System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
    	        */	        
    	        
    	        NodeList listOfRTags = doc.getElementsByTagName("r");
    	        NodeList listOfATags = doc.getElementsByTagName("a");
    	        NodeList listOfDisplayTags = doc.getElementsByTagName("thumb");
    	        
    	        int totalRecs = listOfRTags.getLength();
    	        System.out.println("Total no of recs : " + totalRecs);
    	       
    	        ArrayList alRecords = new ArrayList();
    	        
    	        for( int i = 0; i < totalRecs; i++ ) { 	
    	            // for every attribute
    	            Element rTags = (Element) listOfRTags.item( i );	            
    	            Element aTags = (Element) listOfATags.item( i );
    	            Element displayTags = (Element) listOfDisplayTags.item( i );	            
    	            
    	            try {strRecURL = rTags.getAttribute( "u" );}catch (Exception ex) { }
    	            try {strRecCatPath = rTags.getAttribute( "t" );}catch (Exception ex) { }
    	            try {strRecRank = rTags.getAttribute( "rk" );}catch (Exception ex) { }
    	            try {strRecType = rTags.getAttribute( "g" );}catch (Exception ex) { }
    	            try {
    	            	strRecPrice = aTags.getAttribute( "v" );
    	            	double dblRecPrice = Double.parseDouble(strRecPrice);	            	
    	            	DecimalFormat Currency = new DecimalFormat("$0.00");
    	            	strRecPriceOutput = Currency.format(dblRecPrice);	
    	            }catch (Exception ex) { }
    	            try {strRecThumbnail = displayTags.getAttribute( "v" );}catch (Exception ex) { }
    	            
    	            System.out.println(strRecProductName);
    	            
                	alRecords.add(new String[]{
                								strRecURL, 		//Product URL [0] Element 
    											strRecCatPath, 	//Category Path [1] Element
    											strRecRank, 	//Product Rank [2] Element
    											strRecType, 	//Recommendation Type [3] Element
    											strRecPriceOutput,  	//Product Price [4] Element
    											strRecThumbnail,	// Product Thumbnail [5] Element									
    										});	            
    	        }
    Which works perfectly for the "r" tags, since they are pulling attributes correctly.

    Is there a native XPath for Java? Or should I try Ajax or Javascript? :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,252
    Blog Entries
    1
    Rep Power
    24

    Default

    Is there a native XPath for Java?
    Yes there is. Google has helped me to find information on it and it can help you too. Much luck!

Similar Threads

  1. Undefined attribute name
    By Dieter in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-03-2010, 10:44 AM
  2. Replies: 1
    Last Post: 03-08-2010, 01:34 PM
  3. how to read an attribute of an xml tag in jsp.
    By himacherla in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-21-2009, 06:27 AM
  4. how to add an attribute with sax?
    By cecily in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 04:09 AM
  5. Problem with Attribute in JSP
    By Albert in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-13-2007, 03:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •