100分跪求如何使用JAVA读取XML数据存入数组

[复制链接]
查看11 | 回复0 | 2009-1-30 06:28:38 | 显示全部楼层 |阅读模式
如题,我需要从XML中读取数据,根据数据绘制饼图,我现在的问题是无法从xml中读取数据并存入数组。

                                                                                                读取xml文件内容的程序/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author not attributable * @version 1.0 */import org.w3c.dom.*; import javax.xml.parsers.*;import javax.xml.transform.*;import org.w3c.*;import java.io.*;import javax.xml.transform.TransformerFactory;import javax.xml.transform.Transformer;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult; public class xmltest{
public static void main(String[] args)
{
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("c:/xml.xml");
Element e = doc.getDocumentElement();
NodeList nl = e.getChildNodes();
Node node = doc.getFirstChild();
while( node != null )
{
printNode( node, 0 );
node = node.getNextSibling();
}
}
catch(Exception e){}
}
public static void printNode( Node node, int level )
{
if( node != null )
{
for( int i = level; --i > 0;)
System.out.print( \'\\t\' );
short type = node.getNodeType();
String value = "";
{
if( node.getNodeValue() != null )
value = "\\t value is \\""+ node.getNodeValue() + "\\" ";
System.out.println( node.getNodeName()+ value);
if( node.hasAttributes() )
{
NamedNodeMap nodeMap = node.getAttributes();
for( int i = 0; i < nodeMap.getLength(); i++ )
{
printNode( nodeMap.item( i ), level + 3 );
}
}
}
Node child = node.getFirstChild();
while( child != null )
{
printNode( child, level + 1 );
child = child.getNextSibling();
}
}
} }                                       
提问者对答案的评价:

                                                                                                import java.io.*;
import org.kxml.*; import org.kxml.parser.*; import org.kxml.wap.*;
import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import javax.microedition.io.*;
public class WbxmlTest extends MIDlet implements CommandListener { private Display display = null; private List menu = null;
private Form form = null; private String incomingText = "";
static final Command okCommand
= new Command("Ok",
Command.OK,
1); static final Command exitCommand
= new Command("Exit", Command.EXIT, 0);
// This is a hard coded WSP message that contains // address of web server whereour jsp page is located. byte[] message ={
(byte)\'1\',(byte)0x40,(byte)0x3D,(byte)\'h\',(byte)\'t\',
(byte)\'t\',(byte)\'p\',(byte)\':\',(byte)\'/\',(byte)\'/\',
(byte)\'l\',(byte)\'o\',(byte)\'c\',(byte)\'a\',(byte)\'l\',
(byte)\'h\',(byte)\'o\',(byte)\'s\',(byte)\'t\',(byte)\':\',
(byte)\'8\',(byte)\'0\',(byte)\'8\',(byte)\'0\',(byte)\'/\',
(byte)\'e\',(byte)\'x\',(byte)\'a\',(byte)\'m\',(byte)\'p\',
(byte)\'l\',(byte)\'e\',(byte)\'s\',(byte)\'/\',(byte)\'j\',
(byte)\'s\',(byte)\'p\',(byte)\'/\',(byte)\'f\',(byte)\'i\',
(byte)\'n\',(byte)\'a\',(byte)\'l\',(byte)\'f\',(byte)\'i\',
(byte)\'l\',(byte)\'e\',(byte)\'s\',(byte)\'/\',(byte)\'D\',
(byte)\'a\',(byte)\'t\',(byte)\'.\',(byte)\'j\',(byte)\'s\',
(byte)\'p\',(byte)0x80,(byte)0x94,(byte)0x88,(byte)0x81,
(byte)0x6A,(byte)0x04,(byte)0x83,(byte)0x99
};
// Memory space to receive message. byte[] msg = new byte [256];
public void pauseApp() { /* ----- */ }
public void destroyApp(boolean unconditional)
{ notifyDestroyed(); }
public void startApp() {
display = Display.getDisplay(this);
this.mainMenu(); }//startApp
//Displays the menu screen private void mainMenu() {
menu = new List(" Send Request", Choice.IMPLICIT);
menu.append(" Send Message",null);
menu.addCommand(okCommand);
menu.setCommandListener(this);
display.setCurrent(menu); }//mainMenu
//Display the reply from WAPGateway (JWap). private void showReply() {
form = new Form( "Incoming Message" );
form.append("The price = " + incomingText);
form.addCommand(exitCommand);
form.setCommandListener(this);
display.setCurrent(form); }//showReply
// Makes a WSP Connection with a WAPGateway, // Sends a message and receives the reply. public void getConnect() {
Datagram dgram =null;
DatagramConnection dc=null;
try {
dc = (DatagramConnection)Connector.open ("datagram://127.0.0.1:9200");
dgram = dc.newDatagram(message, message.length);
try{
dc.send(dgram);}
catch (InterruptedIOException e){
e.printStackTrace(); }
dgram = dc.newDatagram (msg,msg.length);
try{
dc.receive(dgram);}
catch (InterruptedIOException e){
e.printStackTrace();}
catch( IOException e){
e.printStackTrace();}
// This is the most interesting part.
incomingText = this.getIncomingTextOfWmlc(dgram.getData());
this.showReply();
dc.close();
}//try
catch (IllegalArgumentException ie){
ie.printStackTrace(); }
catch (ConnectionNotFoundException cnf){
cnf.printStackTrace(); }
catch (IOException e){e.printStackTrace();}
}//getConnect()
private String getIncomingTextOfWmlc ( byte[] wmlc ) {
try {
// Remove WSP header.
// We know it is 19 bytes for our case.
// But for real world applications,
// this should be dynamically deteced.
for ( int j = 0; j < wmlc.length-19; j++ )
wmlc[j] = wmlc[j+19];
WmlParser parser = new WmlParser(new ByteArrayInputStream(wmlc));
while (true) {
try {
ParseEvent parseEvent = parser.read();
if ( parseEvent.getType() == Xml.START_TAG ) {
Attribute attr =
parseEvent.getAttribute("value");
if ( attr != null )
return attr.getValue();
}//if
}//try
catch ( IOException e) {}
}//while
}//try
catch ( IOException e) { e.printStackTrace(); }
return "error"; }//getIncomingTextOfWmlc
public void commandAction(Command c, Displayable d) {
String commandlabel = c.getLabel();
if (commandlabel.equals("Exit"))
destroyApp(false);
else if (commandlabel.equals("Ok"))
getConnect(); }//commandAction }//class WbxmlTest                                       

                                                                                                步骤如下,具体算法就不写了:1.下载jdom的jar 包(当然你也可以用别的xml解析包)加到你的工程lib目录下;2. 建立类引入jdom包,分析你的xml文件;3.将你需要的数据一条条插入数组,注意数组越界。                                        

                                                                                                 startApp() { display = Display.getDisplay(this); this.mainMenu(); }//startApp //Displays the menu screen private void mainMenu() { menu = new List(" Send Request", Choice.IMPLICIT); menu.append(" Send Message",null); menu.addCommand(okCommand); menu.setCommandListener(this); display.setCurrent(menu); }//mainMenu //Display the reply from WAPGateway (JWap). private void showReply() { form = new Form( "Incoming Message" ); form.append("The price = " + incomingText); form.addCommand(exitCommand); form.setCommandListener(this); display.setCurrent(form); }//showReply // Makes a WSP Connection with a WAPGateway, // Sends a message and receives the reply. public void getConnect() { Datagram dgram =null; DatagramConnection dc=null; try { dc = (DatagramConnection)Connector.open ("datagram://127.0.0.1:9200"); dgram = dc.newDatagram(message, message.length); try{ dc.send(dgram);} catch (InterruptedIOException e){ e.printStackTrace(); } dgram = dc.newDatagram (msg,msg.length); try{ dc.receive(dgram);} catch (InterruptedIOException e){ e.printStackTrace();} catch( IOException e){ e.printStackTrace();} // This is the most interesting part. incomingText = this.getIncomingTextOfWmlc(dgram.getData()); this.showReply(); dc.close(); }//try catch (IllegalArgumentException ie){ ie.printStackTrace(); } catch (ConnectionNotFoundException cnf){ cnf.printStackTrace(); } catch (IOException
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行