Results 1 to 1 of 1
Thread: Reading XML from file
- 03-17-2009, 01:34 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 1
- Rep Power
- 0
Reading XML from file
Hello, i am tryin to develop a little program to read xml from a file but the issue i am having is that if a specific tag doesnt exist the program falls over. What i need is some kind of if statement which will allow the program to continue, i just dont know how to go about it.
here is the xml file aswell, any help or atleast a point in the right direction would be greatly appreciatedJava Code:import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import java.io.File; import org.w3c.dom.Document; import org.w3c.dom.*; public class xmlReader{ public static void main (String argv []){ try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse (new File("meta.xml")); // normalize text representation doc.getDocumentElement ().normalize (); System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName()); NodeList listOfPersons = doc.getElementsByTagName("office:meta"); int totalPersons = listOfPersons.getLength(); for(int s=0; s<listOfPersons.getLength() ; s++){ Node nameFilterNode = listOfPersons.item(s); if(nameFilterNode.getNodeType() == Node.ELEMENT_NODE){ Element nameFilterElement = (Element)nameFilterNode; //------- NodeList initialCreatorList = nameFilterElement.getElementsByTagName("meta:initial-creator"); Element initialCreatorElement = (Element)initialCreatorList.item(0); NodeList textInitialCreatorList = initialCreatorElement.getChildNodes(); System.out.println("Initial Creator : " + ((Node)textInitialCreatorList.item(0)).getNodeValue().trim()); //------- NodeList creatorList = nameFilterElement.getElementsByTagName("dc:creator"); Element creatorElement = (Element)creatorList.item(0); NodeList textCreatorList = creatorElement.getChildNodes(); System.out.println("Last Saved By : " + ((Node)textCreatorList.item(0)).getNodeValue().trim()); //---- NodeList printedByList = nameFilterElement.getElementsByTagName("meta:printed-by"); Element printedByElement = (Element)printedByList.item(0); NodeList textPrintedByList = printedByElement.getChildNodes(); System.out.println("Last Printed By : " + ((Node)textPrintedByList.item(0)).getNodeValue().trim()); //------ }//end of if clause }//end of for loop with s var }catch (SAXParseException err) { System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); System.out.println(" " + err.getMessage ()); }catch (SAXException e) { Exception x = e.getException (); ((x == null) ? e : x).printStackTrace (); }catch (Throwable t) { t.printStackTrace (); } //System.exit (0); }//end of main }
XML Code:<?xml version="1.0" encoding="UTF-8" ?> - <office:document-meta> <office:meta> <meta:generator>OpenOffice.org/2.4$Linux OpenOffice.org_project/680m17$Build-9310</meta:generator> <meta:initial-creator>Stacy</meta:initial-creator> <meta:creation-date>2006-12-23T18:53:23</meta:creation-date> <dc:creator>CEIS S2</dc:creator> <dc:date>2009-01-06T14:20:34</dc:date> <dc:language>en-US</dc:language> <meta:editing-cycles>5</meta:editing-cycles> <meta:editing-duration>PT1M13S</meta:editing-duration> <meta:user-defined meta:name="Info 1" /> <meta:user-defined meta:name="Info 2" /> <meta:user-defined meta:name="Info 3" /> <meta:user-defined meta:name="Info 4" /> <meta:document-statistic meta:table-count="0" meta:image-count="0" meta:object-count="0" meta:page-count="128" meta:paragraph-count="6041" meta:word-count="63715" meta:character-count="405653" /> </office:meta> </office:document-meta>
Similar Threads
-
Reading a doc file
By thehero in forum Advanced JavaReplies: 0Last Post: 03-15-2009, 06:48 AM -
help in reading file.
By wanw84 in forum New To JavaReplies: 2Last Post: 10-21-2008, 03:17 PM -
Help reading file
By fritz1474 in forum New To JavaReplies: 4Last Post: 09-17-2008, 03:28 AM -
Right use of file reading ?
By jurka in forum New To JavaReplies: 3Last Post: 08-27-2008, 08:16 PM -
Reading from a file
By leebee in forum New To JavaReplies: 1Last Post: 07-23-2007, 12:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks