Results 1 to 4 of 4
- 07-22-2010, 03:48 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 2
- Rep Power
- 0
Checking for XML Attribute Values
Here is a Snippet from an XML File I have:
My goal is to query the "a" tag. For example, "If n = Title, return XYZ-Blah-Blah"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>
How can I search for where that n tag = "Title" and then return the corresponding v tag value?
Thanks :)
-
I believe that XPath can extract this information for you. What have you tried?
- 07-23-2010, 02:09 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 2
- Rep Power
- 0
So far I have tried the following code:
Which works perfectly for the "r" tags, since they are pulling attributes correctly.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 }); }
Is there a native XPath for Java? Or should I try Ajax or Javascript? :)
-
Yes there is. Google has helped me to find information on it and it can help you too. Much luck!Is there a native XPath for Java?
Similar Threads
-
Undefined attribute name
By Dieter in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-03-2010, 10:44 AM -
Custom Validator for checking duplicate values with database
By tanalyw in forum Web FrameworksReplies: 1Last Post: 03-08-2010, 01:34 PM -
how to read an attribute of an xml tag in jsp.
By himacherla in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 07-21-2009, 06:27 AM -
how to add an attribute with sax?
By cecily in forum New To JavaReplies: 3Last Post: 07-19-2007, 04:09 AM -
Problem with Attribute in JSP
By Albert in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-13-2007, 03:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks