我得到一個xml字符串,我想解析並從中獲取數據。我試圖解析它爲json,但是我得到了空括號。如何解析XML並從XML字符串中獲取數據?
public class ResultsActivity extends Activity {
String outputPath;
TextView tv;
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING;
DocumentBuilder builder;
InputStream is;
Document dom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
setContentView(tv);
String imageUrl = "unknown";
Bundle extras = getIntent().getExtras();
if(extras != null) {
imageUrl = extras.getString("IMAGE_PATH");
outputPath = extras.getString("RESULT_PATH");
}
// Starting recognition process
new AsyncProcessTask(this).execute(imageUrl, outputPath);
}
public void updateResults(Boolean success) {
if (!success)
return;
try {
StringBuffer contents = new StringBuffer();
FileInputStream fis = openFileInput(outputPath);
try {
Reader reader = new InputStreamReader(fis, "UTF-8");
BufferedReader bufReader = new BufferedReader(reader);
String text = null;
while ((text = bufReader.readLine()) != null) {
contents.append(text).append(System.getProperty("line.separator"));
}
} finally {
fis.close();
}
XmlToJson xmlToJson = new XmlToJson.Builder(contents.toString()).build();
// convert to a JSONObject
JSONObject jsonObject = xmlToJson.toJson();
// OR convert to a Json String
String jsonString = xmlToJson.toString();
// OR convert to a formatted Json String (with indent & line breaks)
String formatted = xmlToJson.toFormattedString();
Log.e("xml",contents.toString());
Log.e("json",jsonObject.toString());
} catch (Exception e) {
displayMessage("Error: " + e.getMessage());
}
}
public void displayMessage(String text)
{
tv.post(new MessagePoster(text));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_results, menu);
return true;
}
class MessagePoster implements Runnable {
public MessagePoster(String message)
{
_message = message;
}
public void run() {
tv.append(_message + "\n");
setContentView(tv);
}
private final String _message;
}
}
我跟着這個鏈接:https://github.com/smart-fun/XmlToJson
我只能解析XML?我怎樣才能從XML字符串中獲取數據?
以下是XML字符串:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd" xmlns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd">
<businessCard imageRotation="noRotation">
<field type="Mobile">
<value>•32147976</value>
</field>
<field type="Address">
<value>Timing: 11:00 a.m. to 5.00 p.m</value>
</field>
<field type="Address">
<value>MULTOWECIALITY HOSPITAL Havnmg Hotel MwyantfwfMf), TOL: 1814 7»7»/0454 7575 fax: 2514 MSS MtoMte t wvHwJaMtur0Mapttal.com</value>
</field>
<field type="Name">
<value>M. S. (Surgery), Fais, Fics</value>
</field>
<field type="Company">
<value>KASTURI MEDICARE PVT. LTD.</value>
</field>
<field type="Job">
<value>Consulting General Surgeon Special Interest: Medical Administrator: KsturiSecretary: IMA - Mira</value>
</field>
<field type="Text">
<value>Mob.: •32114976
Dr. Rakhi R
M. S. (Surgery), Surgeon
Special Interest: Medical
President: Bhayander Medical Association
Scientific Secretary: IMA - Mira Bhayander
Timing: 11:00 a.m. to 5.00 p.m
%
*
KASTURI MEDICARE PVT. LTD.
ISO 9001:2008 Certified, ASNH Cliniq 21 Certified,
MtoMte t wvHwJaMtur0Mapttal.com
mkhLkasturi0gmoiH.com</value>
</field>
</businessCard>
我檢查這個鏈接來解析XML:http://androidexample.com/XML_Parsing_-_Android_Example/index.php?view=article_discription&aid=69
但這串劑量沒有名單,我沒有得到如何解析這個xml字符串。任何人都可以幫忙嗎?謝謝..
我會給你建議,將其轉換爲JSON然後解析JsonObject。這比解析XML容易。 –
使用XML PullParser在android中解析XML。 –
這是一個關於使用Json解析數據的完整示例,它可能有所幫助:http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/ –