Results 1 to 1 of 1
- 11-03-2010, 04:35 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
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 04:45 PM.
Similar Threads
-
Methods, JOptionPane, Return Values
By Cubba27 in forum New To JavaReplies: 2Last Post: 12-04-2009, 02:46 AM -
accessing return values from another class
By moaxjlou in forum New To JavaReplies: 3Last Post: 10-31-2008, 02:25 AM -
Using functions that return values?
By Megapixelz in forum New To JavaReplies: 1Last Post: 04-30-2008, 04:07 AM -
how to return values from hashmap
By oregon in forum New To JavaReplies: 2Last Post: 08-01-2007, 04:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks