gpt4 book ai didi

android - android中的本地解析器问题

转载 作者:行者123 更新时间:2023-11-29 22:25:16 26 4
gpt4 key购买 nike

我正在制作一个解析“a.xml”的 3 文件,问题是我得到的最后一行不是完整的解析。那你能帮帮我吗?

package barc3.barc3;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class barc3 extends Activity {

private TextView orgXmlTxt;
private TextView parsedXmlTxt;
private Button parseBtn,chartbtn;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

try {
setContentView(R.layout.main);
orgXmlTxt = (TextView) findViewById(R.id.orgXMLTxt);
parsedXmlTxt = (TextView) findViewById(R.id.parsedXMLTxt);
parseBtn = (Button) findViewById(R.id.parseBtn);
chartbtn =(Button) findViewById(R.id.chartbtn);
//orgXmlTxt.setText(getOriginalMyXML());
parseBtn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
try {
parsedXmlTxt.setText(getParsedMyXML());
} catch (Exception e) {
// TODO Auto-generated catch block
orgXmlTxt.setText(e.getMessage());
}
}
});

chartbtn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

startActivity(new Intent(barc3.this,XMLParsingDemo.class));
}
});

} catch (Exception e) {
orgXmlTxt.setText(e.getMessage());
}
}



/**
* This method is called by OnCreate during startup to show the original XML file content
* @return
* @throws Exception
*/
private String getOriginalMyXML() throws Exception {
/* Load xml file being shown from raw resource folder */
InputStream in = this.getResources().openRawResource(R.raw.a);
StringBuffer inLine = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader inRd = new BufferedReader(isr);

String text;
while ((text = inRd.readLine()) != null) {
inLine.append(text);
inLine.append("\n");
}
in.close();
return inLine.toString();
}

private String getParsedMyXML() throws Exception {
StringBuffer inLine = new StringBuffer();
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();

/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader */
XMLHandler myExampleHandler = new XMLHandler();
xr.setContentHandler(myExampleHandler);
/* Load xml file from raw folder*/
InputStream in = this.getResources().openRawResource(R.raw.a);
/* Begin parsing */
xr.parse(new InputSource(in));
XMLDataSet parsedExampleDataSet = myExampleHandler.getParsedData();
String s = parsedExampleDataSet.getDate();
String s2 = parsedExampleDataSet.getScore();
System.out.println("s is:::::: "+s);
System.out.println("s2 is :::::"+s2);
System.out.println(parsedExampleDataSet);
inLine.append(parsedExampleDataSet.toString());
in.close();
return inLine.toString();
}
}

package barc3.barc3;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class XMLHandler extends DefaultHandler{

// ===========================================================
// Fields
// ===========================================================

private boolean NewDataSet = false;
private boolean Table = false;
private boolean Date = false;
private boolean Score = false;

private XMLDataSet myParsedExampleDataSet = new XMLDataSet();

// ===========================================================
// Getter & Setter
// ===========================================================

public XMLDataSet getParsedData() {
return this.myParsedExampleDataSet;
}

// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
this.myParsedExampleDataSet = new XMLDataSet();
}

@Override
public void endDocument() throws SAXException {
// Nothing to do
}

/** Gets be called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("NewDataSet")) {
this.NewDataSet = true;
}else if (localName.equals("Table")) {
this.Table = true;
}else if (localName.equals("Date")) {
this.Date = true;
}
else if (localName.equals("Score")) {
this.Score = true;
}
}

/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("NewDataSet")) {
this.NewDataSet = false;
}else if (localName.equals("Table")) {
this.Table = false;
}else if (localName.equals("Date")) {
this.Date = false;
}
else if (localName.equals("Score")) {
this.Score = false;
}
}

/** Gets be called on the following structure:
* <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {
if(this.Date)
{
myParsedExampleDataSet.setDate(new String(ch, start, length));
System.out.println("Date"+Date);
}
if(this.Score)
{
myParsedExampleDataSet.setScore(new String(ch, start, length));
System.out.println("Score"+Score);

}

}
}

package barc3.barc3;

public class XMLDataSet {
String Date = null;
String Score = null;

public String getDate() {
return Date;
}
public void setDate(String Date) {
this.Date = Date;
}

public String getScore() {
return Score;
}
public void setScore(String Score) {
this.Score = Score;
}

public String toString(){
return "Date is = " + this.Date
+ "\nScore is = " + this.Score;

}
}

<?xml version="1.0" encoding="utf-8" ?> 
<DataSet xmlns="http://tempuri.org/">
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
- <xs:complexType>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
- <xs:element name="Table">
- <xs:complexType>
- <xs:sequence>
<xs:element name="Date" type="xs:string" minOccurs="0" />
<xs:element name="Score" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<Date>12/5/2011</Date>
<Score>5</Score>
</Table>
- <Table diffgr:id="Table2" msdata:rowOrder="1">
<Date>45/5/2011</Date>
<Score>54</Score>
</Table>
- <Table diffgr:id="Table3" msdata:rowOrder="2">
<Date>12/5/2011</Date>
<Score>12</Score>
</Table>
- <Table diffgr:id="Table4" msdata:rowOrder="3">
<Date>3/4/2011</Date>
<Score>25</Score>
</Table>
- <Table diffgr:id="Table5" msdata:rowOrder="4">
<Date>6/8/2011</Date>
<Score>45</Score>
</Table>
</NewDataSet>
</diffgr:diffgram>
</DataSet>

最佳答案

问题出在您的 DefaultHandler 类中:

myParsedExampleDataSet.setDate(new String(ch, start, length))

characters 方法可以在一个标签上调用多次,例如,如果此标签包含 > 之类的实体,用于 > .

你应该使用 StringBuilder在此方法中,因此您可以将所有新内容附加到它,然后使用 toString() 方法获取您的字符串。

关于android - android中的本地解析器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6151547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com