yngvi
asked on
reading values from XML document
Hi there,
I am new to XML and my java program needs to read the values
from an XML document.
I need to
read specific values and store those values in variables.
Here is the XML Document :
<?xml version="1.0" encoding="utf-8" ?>
- <smsmessages>
- <header>
<nbrmsg>2</nbrmsg>
<error />
- <content>
- <SMS>
<SERVICE>KULA</SERVICE>
<GSM>9874983274</GSM>
<MSG>khjkkj</MSG>
</SMS>
- <SMS>
<SERVICE>KULA</SERVICE>
<GSM>9874983274</GSM>
<MSG>khjkkj</MSG>
</SMS>
</content>
</header>
</smsmessages>
Here is the code so far.. I have already got the whole XML document in the variable "xmlDATA" :
String xmlDATA;
int nrMSG = 0;
sms _sms = new sms();
xmlDATA = _sms.readXMLDocument(URL);
// First I need to read this value "<nbrmsg>2</nbrmsg>" to see how many times I have to loop in the for loop.
for (int i=0; i < nrMSG; i++)
{
String gsmnumber;
String msg;
// here I need get each sms and put it into a variable
}
I am new to XML and my java program needs to read the values
from an XML document.
I need to
read specific values and store those values in variables.
Here is the XML Document :
<?xml version="1.0" encoding="utf-8" ?>
- <smsmessages>
- <header>
<nbrmsg>2</nbrmsg>
<error />
- <content>
- <SMS>
<SERVICE>KULA</SERVICE>
<GSM>9874983274</GSM>
<MSG>khjkkj</MSG>
</SMS>
- <SMS>
<SERVICE>KULA</SERVICE>
<GSM>9874983274</GSM>
<MSG>khjkkj</MSG>
</SMS>
</content>
</header>
</smsmessages>
Here is the code so far.. I have already got the whole XML document in the variable "xmlDATA" :
String xmlDATA;
int nrMSG = 0;
sms _sms = new sms();
xmlDATA = _sms.readXMLDocument(URL);
// First I need to read this value "<nbrmsg>2</nbrmsg>" to see how many times I have to loop in the for loop.
for (int i=0; i < nrMSG; i++)
{
String gsmnumber;
String msg;
// here I need get each sms and put it into a variable
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You need to compile this and create an xml file to pass in as the first agument.
Here's the file I used :
<?xml version="1.0" encoding="utf-8" ?>
<smsmessages>
<header>
<nbrmsg>2</nbrmsg>
<error/>
<content>
<SMS>
<SERVICE>KULA</SERVICE>
<GSM>9874983274</GSM>
<MSG>khjkkj</MSG>
</SMS>
<SMS>
<SERVICE>KULA</SERVICE>
<GSM>9874983274</GSM>
<MSG>khjkkj</MSG>
</SMS>
</content>
</header>
</smsmessages>
Once done you can run it like so :
java XmlTest xmlfile nbrmsg
Basically it creates an xml document and then gets a list of all the elements with the name "nbrmsg". It then gets the first TextElement child of each of the "nbrmsg" elements and prints out its value.
Here's the file I used :
<?xml version="1.0" encoding="utf-8" ?>
<smsmessages>
<header>
<nbrmsg>2</nbrmsg>
<error/>
<content>
<SMS>
<SERVICE>KULA</SERVICE>
<GSM>9874983274</GSM>
<MSG>khjkkj</MSG>
</SMS>
<SMS>
<SERVICE>KULA</SERVICE>
<GSM>9874983274</GSM>
<MSG>khjkkj</MSG>
</SMS>
</content>
</header>
</smsmessages>
Once done you can run it like so :
java XmlTest xmlfile nbrmsg
Basically it creates an xml document and then gets a list of all the elements with the name "nbrmsg". It then gets the first TextElement child of each of the "nbrmsg" elements and prints out its value.
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
public class XEFind{
public static void main(String args[]){
if (args.length < 2){
System.out.println("Usage : java XEFind xmlfile tagname");
System.exit(1);
}
File f = new File(args[0]);
if (f.exists() && f.canRead()){
XMLElementFinder xef = new XMLElementFinder(f);
String s = xef.find(args[1]);
if (s != null){
System.out.println("Elemen t " + args[1] + " found in " + args[0] + ".");
System.out.println("Value = " + s);
}else{
System.out.println("No Element " + args[1] + " found in " + args[0] + ".");
}
}else{
System.out.println(args[0] + " either cannot be found or cannot be read.");
}
}
}
class XMLElementFinder{
DocumentBuilderFactory factory;
DocumentBuilder builder;
Document document;
NodeList nl;
Node n1, n2;
public XMLElementFinder(File xml){
DocumentBuilderFactory factory = DocumentBuilderFactory.new Instance() ;
try{
builder = factory.newDocumentBuilder ();
document = builder.parse(xml);
}catch (Exception e) {
e.printStackTrace();
}
}
public String find(String tagName){
nl = document.getElementsByTagN ame(tagNam e);
if (nl.getLength() > 0){
n1 = nl.item(0);
n2 = n1.getFirstChild();
return n2.getNodeValue();
}else{
return null;
}
}
}
import org.w3c.dom.*;
import java.io.*;
public class XEFind{
public static void main(String args[]){
if (args.length < 2){
System.out.println("Usage : java XEFind xmlfile tagname");
System.exit(1);
}
File f = new File(args[0]);
if (f.exists() && f.canRead()){
XMLElementFinder xef = new XMLElementFinder(f);
String s = xef.find(args[1]);
if (s != null){
System.out.println("Elemen
System.out.println("Value = " + s);
}else{
System.out.println("No Element " + args[1] + " found in " + args[0] + ".");
}
}else{
System.out.println(args[0]
}
}
}
class XMLElementFinder{
DocumentBuilderFactory factory;
DocumentBuilder builder;
Document document;
NodeList nl;
Node n1, n2;
public XMLElementFinder(File xml){
DocumentBuilderFactory factory = DocumentBuilderFactory.new
try{
builder = factory.newDocumentBuilder
document = builder.parse(xml);
}catch (Exception e) {
e.printStackTrace();
}
}
public String find(String tagName){
nl = document.getElementsByTagN
if (nl.getLength() > 0){
n1 = nl.item(0);
n2 = n1.getFirstChild();
return n2.getNodeValue();
}else{
return null;
}
}
}
yngvi:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
[points to ozymandias]
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
I will leave a recommendation in the Cleanup topic area that this question is:
[points to ozymandias]
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
http://java.sun.com/xml/tutorial_intro.html
for an introduction to one of those tools.