Results 1 to 10 of 10
  1. #1
    Paus34 is offline Member
    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Default Need help adding values to array from xml file

    Need help adding values from xml file to array. here is the xml file

    <?xml version="1.0" encoding="UTF-8"?>


    <quote>

    <price>.50</price>
    <price>3.50</price>
    <price>2.50</price>
    </quote>
    Last edited by Paus34; 05-15-2013 at 11:31 AM.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Read in xml file and add values to array

    Use XML decoder?

  3. #3
    Paus34 is offline Member
    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Default Re: Read in xml file and add values to array

    sorry im new to Java can u please explain

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,414
    Rep Power
    5

    Default Re: Read in xml file and add values to array

    I am not an expert at parsing XML. However I have used the sax parser to do so. It is not hard, just tedious. It is probably overkill if your file will remain as simple as you describe. You can use the following example with regular expressions to accomplish what you want.

    Java Code:
          Pattern p = Pattern.compile("<price>(.+)</price>");
          Scanner input = new Scanner(new File("your xml file here"));
          while (input.hasNextLine()) {
             String line = input.nextLine();
             Matcher m = p.matcher(line);
             if (m.matches()) {
                System.out.println(m.group(1)); // or add to list here
             }
          }
    However, there are some caveats. It requires the start and ending tag to be on the same line. It does not do any validation checking of the xml file. It doesn't handle name spaces, etc, etc. For more complex xml files I suggest you read up on the XML processing section in the Java Tutorials. See the link in my signature.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Paus34 is offline Member
    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Default Re: Read in xml file and add values to array

    This is my code so far i cant find a way to catch the price values to add to the ArrayList

    Java Code:
    import java.util.ArrayList;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    
    public class Metric extends DefaultHandler {
    
        
        public static void main(String argv[]) {
            
        try {
     
    	SAXParserFactory factory = SAXParserFactory.newInstance();
    	SAXParser saxParser = factory.newSAXParser();
     
    	DefaultHandler handler = new DefaultHandler() {
     
    	
    	boolean price = false;
                
     
    	public void startElement(String uri, String localName,String qName, 
                    Attributes attributes) throws SAXException {
                    
    		System.out.println("Start Element :" + qName);
     
    		if (qName.equalsIgnoreCase("Price")) {
                          price = true;
                           
    		}
     
    	}
     
    	public void endElement(String uri, String localName,
    		String qName) throws SAXException {
     
    		System.out.println("End Element :" + qName);
     
    	}
     
    	public void characters(char ch[], int start, int length) throws SAXException {
                    
    		if (price) {
                            
    			System.out.println("Price : " + new String(ch, start, length));
    			price = false;
    		}
     
    	}
     
         };
     
           saxParser.parse("C:\\Webservices\\MetricClient\\src\\metricclient\\Prices.xml", handler);
     
         } catch (Exception e) {
           e.printStackTrace();
         }
     
       }
     
    }
    Last edited by Paus34; 05-16-2013 at 09:23 AM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,414
    Rep Power
    5

    Default Re: Read in xml file and add values to array

    You're very close. Try removing setting price to false in the characters() method and place it in the endElement method when the closing tag is detected.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Paus34 is offline Member
    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Default Re: Read in xml file and add values to array

    OK i changed the methods like this added an ArrayList .if i print out he arrylist i only get printed the price tag i want to add the value to the array
    Java Code:
    public void endElement(String uri, String localName,
            String qName) throws SAXException {
            ArrayList prr = new ArrayList();
            System.out.println("End Element :" + qName);
            if (price) {
                prr.add(qName);
                System.out.println("HERE!!!!" + prr);
                price = false;
            }
      
        }
      
        public void characters(char ch[], int start, int length) throws SAXException {
                             
                System.out.println("Price : " + new String(ch, start, length));
                
      
        }
    Last edited by Paus34; 05-20-2013 at 09:41 AM.

  8. #8
    Paus34 is offline Member
    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Default Re: Read in xml file and add values to array

    ok i manage to catch the price the problem i have now is that when the ArrayList print out it seems like the ArrayList Resets

    Java Code:
    package haha;
    
    import java.util.ArrayList;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    
    public class Haha extends DefaultHandler {
       
         
        public static void main(String argv[]) {
             
        try {
      
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
      
        DefaultHandler handler = new DefaultHandler() {
      
         
        boolean bprice = false;
                 
      
        public void startElement(String uri, String localName,String qName, 
                    Attributes attributes) throws SAXException {
                     
            System.out.println("Start Element :" + qName);
      
            if (qName.equalsIgnoreCase("Price")) {
                          bprice = true;
                            
            }
      
        }
      
        public void endElement(String uri, String localName,
            String qName) throws SAXException {
      
            System.out.println("End Element :" + qName);
      
        }
      
        public void characters(char ch[], int start, int length) throws SAXException {
            ArrayList prr = new ArrayList();         
            if (bprice) {
                System.out.println("Price : " + new String(ch, start, length));
                prr.add(new String(ch, start, length));
                
                bprice = false;
            }
            
            System.out.println(prr);
        }
      
       
                
      
         };
      
           saxParser.parse("C:\\Webservices\\MetricClient\\src\\metricclient\\quote.xml", handler);
      
         } catch (Exception e) {
           e.printStackTrace();
         }
      
       }
      
    }

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,414
    Rep Power
    5

    Default Re: Read in xml file and add values to array

    That's because your ArrayList prr is a local variable and is redeclared each time characters() is invoked. Make prr an instance variable of your class. Only assign values to it in the characters() method. Don't re-allocate.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    Paus34 is offline Member
    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Default Re: Read in xml file and add values to array

    thanks it works now

Similar Threads

  1. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  2. Read File into 2d array
    By almjodla in forum New To Java
    Replies: 8
    Last Post: 03-23-2010, 02:55 PM
  3. Replies: 40
    Last Post: 07-04-2008, 08:41 AM
  4. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 02:22 PM
  5. How to read attributes and values in a xml file using servlet
    By pragathi_forum in forum Advanced Java
    Replies: 1
    Last Post: 12-18-2007, 05:46 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
  •