Results 1 to 6 of 6
Thread: How to read my xml file
- 04-30-2011, 05:31 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
How to read my xml file
Hi I have to read a xml file using with the java.
I want to get the values of nm and nbr attributes.XML Code:<?xml version="1.0" encoding="ISO-8859-1" ?> <form TOKEN="GL40.1" TITLE="Journal Entry" FORMTYPE="form" =OTKNXFER="0" TYPE="ONLINE" SYSTEM="GL" RUNTYPE="lapm" =iddenFC="0" defaultFC="I" line="LW" keycheck="CXR" =atareq="AC" next="NP" add="A" lreq="ACX" chg="C" lchg="C" =datareq="AC" inq="I" ladd="A" pairs="AACACCCDIXX1X2X3X4X5X6" =MLGenBy="xscrgen 9.0.0.5.215 2008-04-24 04:00:00 (200805)" =MLGenDT="20100624 0852"> <fld tp="Hidden" nbr="_f2" sz="5" nm="TRANSFER-FROM" al="left" =d="upper" keynbr="TRE" nknimp="1"/> <fld tp="Hidden" nbr="_f3" sz="1" nm="PT-XMIT-NBR" al="left" =d="numeric"/> <fld tp="Hidden" nbr="_f4" sz="6" nm="PT-LINE-AFT-INS" =l="left" ed="numeric"/> <fld tp="Hidden" nbr="_f5" sz="1" nm="PT-INSERT" al="left" =d="upper"/> <fld tp="Hidden" nbr="_f6" sz="1" nm="PT-MIMIC-ENTRY" =l="left" ed="upper" keynbr="GFI"/> <fld tp="Hidden" nbr="_f7" sz="17" mxsz="20" decsz="2" =m="GLC-ENTRY-DB" al="left" ed="signed" keynbr="GkI" =knimp="1"/> <fld tp="Hidden" nbr="_f8" sz="17" mxsz="20" decsz="2" =m="GLC-ENTRY-CR" al="left" ed="signed" keynbr="GkH" =knimp="1"/> <fld tp="Hidden" nbr="_f9" sz="17" mxsz="20" decsz="2" =m="SV-ENTRY-DIFF" al="left" ed="signed" keynbr="GkC"/> </form> </xml>
Please help me in reading the xml fileLast edited by Fubarable; 04-30-2011 at 11:19 PM. Reason: html tags added
-
What has Google told you, and what have you tried? Have you tried XPath? If not, I'd start with that if all you want to do is extract out specific information and are not interested in creating an XML file.
- 04-30-2011, 09:56 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 31
- Rep Power
- 0
I think this simple example will help you
XML Code:<?xml version="1.0" encoding="windows-1252" ?> <LoginInfo> <Users> <username>John</username> <password>password</password> <autologin>false</autologin> <dbfileloc>null</dbfileloc> </Users> </LoginInfo>Java Code:try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlfile); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("Users"); for (int i = 0; i < nList.getLength(); i++) { Node nNode = nList.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element)nNode; username = getTagValue("username", eElement); password = getTagValue("password", eElement); autologin = Boolean.parseBoolean(getTagValue("autologin", eElement)); dbfileloc = getTagValue("dbfileloc", eElement); } } } catch (NullPointerException e) { e.toString(); }catch(Exception e){ e.printStackTrace(); } //return the value of the subnode in a sting private static String getTagValue(String sTag, Element eElement){ NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes(); Node nValue = nlList.item(0); return nValue.getNodeValue(); }Last edited by oomrichie; 04-30-2011 at 10:55 PM.
-
- 04-30-2011, 10:49 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 31
- Rep Power
- 0
Sorry I Forgot to include the method. my Bad :D
Java Code://return the value of the subnode in a sting private static String getTagValue(String sTag, Element eElement){ NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes(); Node nValue = nlList.item(0); return nValue.getNodeValue(); }
- 05-23-2011, 10:34 AM #6
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Write a program that sorts data from a text file and sort them in a file
By danmgz45 in forum New To JavaReplies: 6Last Post: 12-01-2010, 05:31 AM -
pack200 .gz file unpack and create .jar file in browser(speed test).
By maheshsardar in forum Java AppletsReplies: 1Last Post: 08-04-2010, 03:24 PM -
Sending a File from Server to Client and saving the file to Clients computer
By al_Marshy_1981 in forum NetworkingReplies: 8Last Post: 02-18-2010, 12:54 PM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
How to parse the CSV(Comma separation values)file and validate the file using java
By padmajap13 in forum Advanced JavaReplies: 7Last Post: 05-23-2008, 03:46 AM


LinkBack URL
About LinkBacks


Bookmarks