Results 1 to 1 of 1
  1. #1
    Kenjitsuka is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default Having XML readerclass return values, but which way is best?

    Hello all,

    I wrote a class a while back, which gets a specific set of values from specific XML files. It can then print the results in Console and it works just fine.
    But of course I didn't make a class to print to console ;)

    I need to get the values into a database. There is a separate class that handles the db input.
    What I'd like to ask you guys is, what way is the simplest or otherwise best?
    I was thinking of a String[], but am having trouble returning anything at all...

    For as far as I know the return statement should be outside the current method (after the main method ends), but then I can't get the values from inside the main method that reads them from the XML (the values from the Nodelist 'nodes'). I was thinking of using a bunch of global variables, but it seems a rather awkward thing to do.

    Maybe split the whole thing into two methods; one for reading which can be called from another class and a second method that can be read by the database-put class?

    I'm not a very good coder, let alone experienced, so I'd value your input highly! :)

    Below is the class as it is now:

    Java Code:
    import org.w3c.dom.*;
    import javax.xml.xpath.*;
    import javax.xml.parsers.*;
    import java.io.IOException;
    import org.xml.sax.SAXException;
    
    public class xmlGrabber {
    
      public static void main(String[] args) 
       throws ParserConfigurationException, SAXException, 
              IOException, XPathExpressionException {
    
        DocumentBuilderFactory domFactory = 
        DocumentBuilderFactory.newInstance();
              domFactory.setNamespaceAware(true); 
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        String docuInvoer = "C:/temp/document.xml";
        Document doc = builder.parse(docuInvoer);
        XPath xpath = XPathFactory.newInstance().newXPath();
           // XPath Query for showing all nodes value
        XPathExpression expr = xpath.compile("//@fieldvalue");
    
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        for (int i = 1; i < 24; i++) {
         System.out.println(nodes.item(i).getNodeValue()); 
         //String bla = nodes.item(1).getNodeValue(); 
        
        }
      }
    }
    Last edited by Kenjitsuka; 11-03-2010 at 05:45 PM.

Similar Threads

  1. Methods, JOptionPane, Return Values
    By Cubba27 in forum New To Java
    Replies: 2
    Last Post: 12-04-2009, 03:46 AM
  2. accessing return values from another class
    By moaxjlou in forum New To Java
    Replies: 3
    Last Post: 10-31-2008, 03:25 AM
  3. Using functions that return values?
    By Megapixelz in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 05:07 AM
  4. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:56 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
  •