我想将XML解析为我的Android应用。我试过一个教程。在这里它使用DOMParser。应用程序中没有错误。但它没有显示XML输出。请帮我解决这个问题。这是我现在尝试的。
<?php
require_once($_SERVER["DOCUMENT_ROOT"].'/layout/layout.inc.php');
require_once($_SERVER["DOCUMENT_ROOT"].'/functions/general.inc.php');
$layout = new default_layout();
$layout->title('IT KB Search');
$layout->content("<div class='border'>");
$layout->content('<h1>IT Support Knowledge Base - Search Results</h1>');
if (isset($_GET['q'])) {
$query = rawurlencode( strip_tags($_GET['q']));
$timestamp = time();
$baseUrl = 'https://oursite.atlassian.net/wiki';
$url = $baseUrl.'/rest/api/content/search?cql=space=KB%20AND%20type=page%20AND%20title~'.$query;
// To enable authenticated search:
// $url .= "&os_username=$username&os_password=$password";
$response = file_get_contents($url);
$response = json_decode($response);
$results = $response->results;
# Change Starts
$html = '<div>';
$html .= '<ol>';
foreach($results as $item) {
$html .= '<li><strong><a href="';
$html .= $baseUrl. $item-> _links-> webui;
$html .= '" target="_blank">';
$html .= $item->title;
$html .= '</a></strong></li>';
}
$html .= '</ol></div><hr>';
$html .= '</div>';
$layout->content($html);
# Change Ends
}
$layout->content('</div>');
$layout->render();
答案 0 :(得分:0)
您无法在UI/Main
线程上执行网络或长时间运行任务,因此您需要使用AsyncTask
在后台线程中执行该任务。尝试从AsyncTask
中的url加载xml,然后将xml加载到UI线程中的布局中。
public class XMLParsingDOMExample extends Activity {
LinearLayout layout;
TextView name[];
TextView website[];
TextView category[];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
loadXML("http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
}
private void loadXML(final String stringUrl){
new AsyncTask<Void,Void,Document>(){
@Override
protected Document doInBackground(Void... params) {
try {
URL url = new URL(stringUrl);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
return db.parse(new InputSource(url.openStream()));
}catch (Throwable e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Document document) {
super.onPostExecute(document);
parseXML(document);
}
}.execute();
}
private void parseXML(Document doc){
try {
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
name = new TextView[nodeList.getLength()];
website = new TextView[nodeList.getLength()];
category = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
name[i] = new TextView(this);
website[i] = new TextView(this);
category[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("name");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
name[i].setText("Name = "
+ ((Node) nameList.item(0)).getNodeValue());
NodeList websiteList = fstElmnt.getElementsByTagName("website");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
website[i].setText("Website = "
+ ((Node) websiteList.item(0)).getNodeValue());
category[i].setText("Website Category = "
+ websiteElement.getAttribute("category"));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
setContentView(layout);
}
}