我有这个代码,这是有效的,但问题出在这个 example.xml 文件中,在这个文件中单值单行被接受。
(value>Northland College, 1411 Ellis Avenue, Ashland, Wisconsin</value>)
如果我们给出多行,则只需要读取一行。
<value>Northland Community and Technical College has been in existence in one form or other since 1949,
when the Minnesota State Board of Education approved an area vocational school for Thief River Falls.</value>
但我想多行阅读。请给我一个答案
例子.xml
<list>
<lists name="clg_history">
<tag>College_History</tag>
<value>Northland Community and Technical College has been in existence in one form or other since 1949,
when the Minnesota State Board of Education approved an area vocational school for Thief River Falls.</value>
</lists>
<lists name="clg_courses">
<tag>College_Courses</tag>
<value>BTECH: CSC,IT, Civil Eng, Chemical Eng, Mechanical Eng, EEE,
PG: MCA, MBA</value>
</lists>
<lists name="clg_address">
<tag>College_Address</tag>
<value>Northland College, 1411 Ellis Avenue, Ashland, Wisconsin</value>
</lists>
<lists name="clg_contact">
<tag>College_Contact_Details</tag>
<value>54806-3999,
(715) 682-1699</value>
</lists>
<lists name="clg_logo">
<tag>College_Logo</tag>
<value>http://www.logotypes101.com/logos/626/EC8E12391BD7F7C504C68C89A0E93484/Canisius_College.png</value>
</lists>
</list>
MyXMLHandler.java
public class MyXMLHandler extends DefaultHandler {
Boolean currentElement = false;
String currentValue=null;
public static SitesList sitesList = null;
public static SitesList getSitesList() {
return sitesList;
}
public static void setSitesList(SitesList sitesList) {
MyXMLHandler.sitesList = sitesList;
}
@Override
public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("list"))
{
/** Start */
sitesList = new SitesList();
}
}
/** Called when tag closing ( ex:- <name>AndroidPeople</name>
* -- </name> )*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
currentElement = false;
/** set value */
if (localName.equalsIgnoreCase("tag"))sitesList.setTag(currentValue);
else if (localName.equalsIgnoreCase("value"))sitesList.setValue(currentValue);
}
/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
* -- to get AndroidPeople Character ) */
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}
站点列表.java
public class SitesList {
/** Variables */
private static ArrayList<String> tag = new ArrayList<String>();
private static ArrayList<String> value = new ArrayList<String>();
private static String ctag;
private static String cvalue;
private static String clg_history_tag;
private static String clg_history_value;
private static String clg_address_tag;
private static String clg_address_value;
private static String clg_courses_tag;
private static String clg_courses_value;
private static String clg_contact_tag;
private static String clg_contact_value;
private static String clg_logo_tag;
private static String clg_logo_value;
/** In Setter method default it will return arraylist
* change that to add */
public ArrayList<String> getTag() {
return tag;
}
public void setTag(String name) {
tag.add(name);
}
public ArrayList<String> getValue() {
return value;
}
public void setValue(String value1) {
value.add(value1);
}
public String getClgHistoryTag()
{
return clg_history_tag;
}
public void setClgHistoryTag(String currentValue) {
clg_history_tag=currentValue;
}
public String getClgHistoryValue()
{
return clg_history_value;
}
public void setClgHistoryValue(String currentValue) {
clg_history_value=currentValue;
}
public String getClgAddressTag()
{
return clg_address_tag;
}
public void setClgAddressTag(String currentValue) {
clg_address_tag=currentValue;
}
public String getClgAddressValue()
{
return clg_address_value;
}
public void setClgAddressValue(String currentValue) {
clg_address_value=currentValue;
}
public String getClgCoursesTag()
{
return clg_courses_tag;
}
public void setClgCoursesTag(String currentValue) {
clg_courses_tag=currentValue;
}
public String getClgCoursesValue()
{
return clg_courses_value;
}
public void setClgCoursesValue(String currentValue) {
clg_courses_value=currentValue;
}
public String getClgContactTag()
{
return clg_contact_tag;
}
public void setClgContactTag(String currentValue) {
clg_contact_tag=currentValue;
}
public String getClgContactValue()
{
return clg_contact_value;
}
public void setClgContactValue(String currentValue) {
clg_contact_value=currentValue;
}
public String getClgLogoTag()
{
return clg_logo_tag;
}
public void setClgLogoTag(String currentValue) {
clg_logo_tag=currentValue;
}
public String getClgLogoValue()
{
return clg_logo_value;
}
public void setClgLogoValue(String currentValue) {
clg_logo_value=currentValue;
}
}
xmlparser.java
public class Xmlparser extends Activity {
SitesList sitesList = null;
@Override
protected void onCreate(Bundle TravisLoveBacon) {
super.onCreate(TravisLoveBacon);
// making it full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
TextView tag[];
TextView value[];
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
InputSource is = new InputSource(getResources().openRawResource(R.raw.example));
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(is.getByteStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
//System.out.println("");
/** Assign textview array lenght by arraylist size */
tag = new TextView[sitesList.getTag().size()];
System.out.println("tags"+sitesList.getTag().size());
value = new TextView[sitesList.getValue().size()];
System.out.println("values"+sitesList.getValue().size());
/** Set the result text in textview and add it to layout */
sitesList.setClgHistoryTag(sitesList.getTag().get(0));
sitesList.setClgHistoryValue(sitesList.getValue().get(0));
sitesList.setClgCoursesTag(sitesList.getTag().get(1));
sitesList.setClgCoursesValue(sitesList.getValue().get(1));
sitesList.setClgAddressTag(sitesList.getTag().get(2));
sitesList.setClgAddressValue(sitesList.getValue().get(2));
sitesList.setClgContactTag(sitesList.getTag().get(3));
sitesList.setClgContactValue(sitesList.getValue().get(3));
sitesList.setClgLogoTag(sitesList.getTag().get(4));
sitesList.setClgLogoValue(sitesList.getValue().get(4));
System.out.println(sitesList.getClgHistoryValue());
System.out.println(sitesList.getClgCoursesValue());
System.out.println(sitesList.getClgAddressValue());
System.out.println(sitesList.getClgContactValue());
System.out.println(sitesList.getClgLogoValue());
}
@Override
protected void onPause() {
super.onPause();
finish();
}
}