Results 1 to 6 of 6
  1. #1
    lshankar_mca is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default How to read my xml file

    Hi I have to read a xml file using with the java.

    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>
    I want to get the values of nm and nbr attributes.

    Please help me in reading the xml file
    Last edited by Fubarable; 04-30-2011 at 11:19 PM. Reason: html tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  3. #3
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by oomrichie View Post
    I think this simple example will help you

    .....

    Java Code:
                        //....
    
                        username = getTagValue("username", eElement);
    
                        //....
    Pardon my ignorance, but what API is the getTagValue method from?

  5. #5
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

    Default

    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();
        }

  6. #6
    danday is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  2. Replies: 1
    Last Post: 08-04-2010, 03:24 PM
  3. Replies: 8
    Last Post: 02-18-2010, 12:54 PM
  4. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  5. Replies: 7
    Last Post: 05-23-2008, 03:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •