Results 1 to 12 of 12
Thread: Basics of XML Parsing
- 04-18-2011, 03:04 PM #1
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
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)
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:<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 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.javaJava Code:private String privateIdentifier; private int msisdn; private String prefDomain; private String prefAuthScheme;
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.
- 04-18-2011, 03:06 PM #2
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!
- 04-18-2011, 04:53 PM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
@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.
- 04-19-2011, 03:16 AM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
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.
When I run the above code I get the following result.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()); } } } }
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.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
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.
- 04-19-2011, 12:43 PM #5
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. :pHow to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-19-2011, 12:55 PM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
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 :)
-
Does your XML file have an associated schema file?
Here's one decent link on jaxb: JAXB Reference Implementation — Java.net
- 04-19-2011, 01:06 PM #8
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-19-2011, 01:09 PM #9
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
@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.
- 04-19-2011, 01:13 PM #10
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
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.
- 04-19-2011, 01:15 PM #11
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-19-2011, 01:25 PM #12
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
Similar Threads
-
multithreading basics
By nikkka in forum New To JavaReplies: 2Last Post: 04-14-2011, 01:55 PM -
Anyone willing to help me with Java basics?
By Rocketz in forum New To JavaReplies: 7Last Post: 02-23-2011, 04:30 AM -
Basics
By avish12 in forum SWT / JFaceReplies: 2Last Post: 06-09-2010, 03:04 PM -
Really Basics
By Taluntain in forum New To JavaReplies: 16Last Post: 10-08-2009, 09:43 AM -
Basics
By AKP in forum New To JavaReplies: 7Last Post: 05-23-2008, 12:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks