我正在尝试从一个网址解析数据,但它没有在我的屏幕上显示解析数据:
public class XML_Parsing_Sample extends UiApplication {
//creating a member variable for the MainScreen
MainScreen _screen= new MainScreen();
//string variables to store the values of the XML document
String _node,_element;
Connection _connectionthread;
public XML_Parsing_Sample() {
// TODO Auto-generated constructor stub
_screen.setTitle("XML Parsing");//setting title
_screen.add(new RichTextField("Requesting....."));
_screen.add(new SeparatorField());
//_screen.add(new RichTextField("XML data"));
pushScreen(_screen); // creating a screen
"System.out.println("1111111111111");"
//creating a connection thread to run in the background
_connectionthread = new Connection();
"System.out.println("222222222222222");"
_connectionthread.start();//starting the thread operation
"System.out.println("After connection run");"
}
public void updateField(String node, String element){
//receiving the parsed node and its value from the thread
//and updating it here
//so it can be displayed on the screen
String title="Title";
_screen.add(new RichTextField(node+" : "+element));
if(node.equals(title)){
_screen.add(new SeparatorField());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
XML_Parsing_Sample application = new XML_Parsing_Sample();
//create a new instance of the application
//and start the application on the event thread
application.enterEventDispatcher();
}}
为了在模拟器上显示解析数据,我使用下面的代码,但它不起作用。
public class Connection extends Thread {
public Connection() {
// TODO Auto-generated constructor stub
super();
" System.out.println("outside run method");"
}
public void run(String _node, String _element){
"System.out.println("inside run method");"
// define variables later used for parsing
Document doc;
StreamConnection conn;
try{
//providing the location of the XML file,
//your address might be different
conn=(StreamConnection)Connector.open
("http://magazine.ateemo.com/magazines/by_publisher/2");
//next few lines creates variables to open a
//stream, parse it, collect XML data and
//extract the data which is required.
//In this case they are elements,
//node and the values of an element
DocumentBuilderFactory docBuilderFactory
= DocumentBuilderFactory. newInstance();
DocumentBuilder docBuilder
= docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
doc = docBuilder.parse(conn.openInputStream());
doc.getDocumentElement ().normalize ();
NodeList list=doc.getElementsByTagName("*");
_node=new String();
_element = new String();
//this "for" loop is used to parse through the
//XML document and extract all elements and their
//value, so they can be displayed on the device
for (int i=0;i<list.getLength();i++){
Node value=list.item(i).
getChildNodes().item(0);
_node=list.item(i).getNodeName();
_element=value.getNodeValue();
updateField(_node,_element);
}//end for
}//end try
//will catch any exception thrown by the XML parser
catch (Exception e){
System.out.println(e.toString());
}
}//end connection function
private void updateField(String _node, String _element) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
第一步是弄清楚问题是解析本身,还是显示解析结果。我将首先添加调试打印输出以查看解析器是否正常工作。这样您就可以将问题分解为代码的一小部分。