Results 1 to 12 of 12
  1. #1
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default Basics of XML Parsing

    Hi friends,

    I'm new to XML-Parsing. I'd require your help to start off. My project has the following XML (not full)
    Java Code:
    <privateIdentity>
    	<privateId>h9tqv1s@vodafone-3gpp.org</privateId>
    	<msisdn>989845682812</msisdn>
    	<preferredDomain>IMS</preferredDomain>
    	<preferredAuthScheme>AKA</preferredAuthScheme>
    </privateIdentity>
    <ImplicitRegesteredSet>
    	<IrsName>IRS__1</IrsName>
    	<registrationStatus>N</registrationStatus>
    </ImplicitRegesteredSet>
    <publicIdentity>
    	<publicId>sip:sp123@vodafone-3gpp.org</publicId>
    	<IrsName>IRS__1</IrsName>
    	<isDefault>true</isDefault>
    </publicIdentity>
    I have the following classes where each of the values have to be populated. For example, I have PrivateIdentity.java where I have the following private variables defined.
    Java Code:
    	private String privateIdentifier;
    	private int msisdn;
    	private String prefDomain;
    	private String prefAuthScheme;
    I will also have the corresponding getters and setters. As it is very obvious my job is to get the value from the XML data, for example, from the XML, I should get MSISDN as 989845682812 and put this value in the msisdn variable in PrivateIdentity.java

    I am not asking for a complete code. Just the right API to use for Parsing this XML would be enough for me.

    Thanks in advance.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Are you looking for JAXB?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    @KevinWorkman:
    I'm not sure about what JAXB is.
    I had a little bit of googling after posting this and from that I think I need to make use of DOMs here. I don't know how it is but I read that it has to do something with parsing.

  4. #4
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Unhappy

    Hi,
    Now I definitely require somebody's help. I wrote a code which does the parsing fine and I'm able to fetch the values of each XML tag.
    The contents of parseme.txt is exactly same as the XML I have posted in the first post.

    Java Code:
    public class DocumentTest {
    
    	public static void main(String[] args) {
    		String script = "D://premdas/Desktop/file_tutorials/parseme.txt";
    		DocumentBuilderFactory obj = DocumentBuilderFactory.newInstance();
    		try {
    			DocumentBuilder db =obj.newDocumentBuilder();
    			Document doc = db.parse(new File(script));
    			if (doc != null) {
    				NodeList nodes = doc.getChildNodes();
    				if (nodes != null) {
    					iterate(nodes);
    				}
    			}
    		} catch (ParserConfigurationException e) {
    			e.printStackTrace();
    		} catch (SAXException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	private static void iterate(NodeList nodeList) {
    		Node nodeitem;
    		for (int i=0; (nodeitem = nodeList.item(i)) != null; i++) {
    			if (nodeitem.getNodeType() == Document.ELEMENT_NODE) {
    				iterate(nodeitem.getChildNodes());
    			} else if (nodeitem.getNodeType() == Document.TEXT_NODE){
    				System.out.print(nodeitem.getParentNode().getParentNode().getNodeName()+" : ");
    				System.out.println(nodeitem.getParentNode().getNodeName()+"="+nodeitem.getNodeValue());
    			}
    		}
    	}
    }
    When I run the above code I get the following result.
    Java Code:
    privateIdentity : privateId=h9tqv1s@vodafone-3gpp.org
    privateIdentity : msisdn=989845682812
    privateIdentity : preferredDomain=IMS
    privateIdentity : preferredAuthScheme=AKA
    ImplicitRegesteredSet : IrsName=IRS__1
    ImplicitRegesteredSet : registrationStatus=N
    publicIdentity : publicId=sip:sp123@vodafone-3gpp.org
    publicIdentity : IrsName=IRS__1
    publicIdentity : isDefault=true
    Now I'm stuck with the mapping part. How do I map XML tag "privateId" to the privateIdentifier in PrivateIdentity.java and similarly XML tag publicId to be mapped to the corresponding String variable in PublicIdentity.java and so on.
    I could have written if() statements for each XML tag, but I somehow felt there will be an easier and better approach. Can someone please help.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Are you just asking how to assign variables to the values you're printing out here?

    By the way, JAXB would do all of this for you. Just saying. :p
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Are you just asking how to assign variables to the values you're printing out here?
    Yes. Thats exactly what I want. As mentioned in the first post I have a class called PrivateIdentity.java where I have getters and setters for private members. I need to put these values that I'm printing to these variables. This is for values under privateIdentity tag. For values under ImplicitRegesteredSet tag I have another class.

    I'll also give a try on JAXB. Anywhere I get blocked i'll reply to this post. Meantime if there is any good links to study JAXB that you can share, it'll be great :)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Quote Originally Posted by subith86 View Post
    Yes. Thats exactly what I want. As mentioned in the first post I have a class called PrivateIdentity.java where I have getters and setters for private members. I need to put these values that I'm printing to these variables. This is for values under privateIdentity tag. For values under ImplicitRegesteredSet tag I have another class.
    I guess I'm confused. Why can't you just call the setters with the values instead of printing them out?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    @Fubarable: Thanks for the link. I'll go through it.
    In the actual testbed, there is a schema. But I'm doing this in my local machine. I don't have a schema here. I have only the XML script stored in one text file.

  10. #10
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Why can't you just call the setters with the values instead of printing them out?
    I could have done that but, in that case I'd require lot of "if" statements for calling the corresponding setters. In future if a new tag is introduced, i'll have to change the code also. Maybe this is how it is done, but I just wanted to know if there is any better way.

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Quote Originally Posted by subith86 View Post
    I could have done that but, in that case I'd require lot of "if" statements for calling the corresponding setters. In future if a new tag is introduced, i'll have to change the code also. Maybe this is how it is done, but I just wanted to know if there is any better way.
    Yeah, sorry to repeat myself, but it really sounds like you're looking for JAXB.

    I guess you could somehow use a Map of tag names to values, but that would probably be a hack.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    yeah:D Even I started to realise ;)
    Let me learn JAXB. Then I'll repost here.
    Thanks

Similar Threads

  1. multithreading basics
    By nikkka in forum New To Java
    Replies: 2
    Last Post: 04-14-2011, 02:55 PM
  2. Anyone willing to help me with Java basics?
    By Rocketz in forum New To Java
    Replies: 7
    Last Post: 02-23-2011, 05:30 AM
  3. Basics
    By avish12 in forum SWT / JFace
    Replies: 2
    Last Post: 06-09-2010, 04:04 PM
  4. Really Basics
    By Taluntain in forum New To Java
    Replies: 16
    Last Post: 10-08-2009, 10:43 AM
  5. Basics
    By AKP in forum New To Java
    Replies: 7
    Last Post: 05-23-2008, 01:06 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
  •