Java android don'的XML解析器;我看不懂最后一个孩子
我的android手机中的解析器有问题! 下面是XML的代码Java android don'的XML解析器;我看不懂最后一个孩子,java,android,xml,xml-parsing,sd-card,Java,Android,Xml,Xml Parsing,Sd Card,我的android手机中的解析器有问题! 下面是XML的代码 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <file> <Staff> <connection>wifi</connection> <timestamp>20</timestamp> <sport>0</spor
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<file>
<Staff>
<connection>wifi</connection>
<timestamp>20</timestamp>
<sport>0</sport>
</Staff>
<Staff>
<connection>3g</connection>
<timestamp>40</timestamp>
<sport>0</sport>
</Staff>
<Staff>
<connection>wifi</connection>
<timestamp>60</timestamp>
<sport>0</sport>
</Staff>
<Staff>
<connection>3g</connection>
<timestamp>80</timestamp>
<sport>0</sport>
</Staff>
</file>
无线网络
20
0
3g
40
0
无线网络
60
0
3g
80
0
这是我的解析器代码
try {
InputStream filename = null;
Document obj_doc = null;
DocumentBuilderFactory doc_build_fact = null;
DocumentBuilder doc_builder = null;
filename = new FileInputStream("/sdcard/data.xml");
doc_build_fact = DocumentBuilderFactory.newInstance();
doc_builder = doc_build_fact.newDocumentBuilder();
System.out.println("readed data.xml");
obj_doc = doc_builder.parse(filename);
NodeList obj_nod_list = null;
if (null != obj_doc) {
org.w3c.dom.Element feed = obj_doc.getDocumentElement();
obj_nod_list = feed.getElementsByTagName("file");
}
Element root = obj_doc.getDocumentElement();
NodeList items = root.getElementsByTagName("Staff");
System.out.println("items "+items.getLength());
for (int i = 0; i < items.getLength(); i++) {
Node item = items.item(i);
NodeList properties = item.getChildNodes();
System.out.println("properties length "+item.getChildNodes().getLength());
// System.out.println("properties "+properties.getLength());
for (int j = 0; j < items.getLength(); j++) {
Node property = properties.item(j);
// System.out.println("properties "+property.getNodeName());
String name = property.getNodeName();
if (name.equalsIgnoreCase("connection")) {
// Store it where you want
connection.add(property.getFirstChild().getNodeValue());
System.out.println("connection "+connection);
// System.out.println("connection "+connection.get(i));
}
if (name.equalsIgnoreCase("timestamp")) {
int inttimestamp = Integer.parseInt(property.getFirstChild().getNodeValue());
timestamp.add(inttimestamp);
System.out.println("timestamp "+timestamp);
}
if (name.equalsIgnoreCase("sport")) {
int inttimestamp = Integer.parseInt(property.getFirstChild().getNodeValue());
capacity.add(inttimestamp);
System.out.println("capacity "+capacity);
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
试试看{
InputStream文件名=null;
单据对象单据=null;
DocumentBuilderFactory文档构建事实=null;
DocumentBuilder doc\u builder=null;
filename=newfileinputstream(“/sdcard/data.xml”);
doc_build_fact=DocumentBuilderFactory.newInstance();
doc_builder=doc_build_fact.newDocumentBuilder();
System.out.println(“readed data.xml”);
obj_doc=doc_builder.parse(文件名);
NodeList obj_nod_list=null;
如果(空!=对象文档){
org.w3c.dom.Element feed=obj_doc.getDocumentElement();
obj_nod_list=feed.getElementsByTagName(“文件”);
}
Element root=obj_doc.getDocumentElement();
nodelistitems=root.getElementsByTagName(“Staff”);
System.out.println(“items”+items.getLength());
对于(int i=0;i
当解析完成时!它看不懂最后一个孩子!这就是运动!你能给我一个解决方案吗!另外,当我更改xml并将“sport”放在第一位时,它无法读取最后一个节点!感谢使用jsoup html解析器。您可以使用它解析html、xml等。它是一个基于标记的解析器 下面是一个使用SAX解析器的示例,它可以帮助您 解析器类:
public class XMLParser extends DefaultHandler
{
// xml Tags name
private final String TENDER_TYPE = "TenderType";
private final String TRX_TYPE = "TransactionType";
private final String DATA_ELEM = "DataElement";
//xml Values
private final String NAME = "Name";
private final String VALUE = "Value";
private final String AMOUNT = "Amount";
private final String T_TYPE = "TenderType";
private final String CLRK_ID = "ClerkId";
private final String INV_NUM = "InvoiceNum";
private final String AUTH = "Authorization";
private final String ORIG_SEQ = "OriginalSequence";
private final String ORIG_REF = "OriginalReference";
private final String TAG = "Tag";
private final String DESCRIPTION = "Description";
//list for imported Config data
private ArrayList<TenderType> theTenderTypeList = null;
private TenderType currentTenderType = null;
private ArrayList<TransactionType> theTrxTypeList = null;
private TransactionType currentTrxType = null;
private ArrayList<DataElement> theDataElementList = null;
private DataElement currentDataElement = null;
@Override
public void startDocument() throws SAXException
{
super.startDocument();
if(theTenderTypeList == null)
{
theTenderTypeList = new ArrayList<TenderType>();
}
if(theTrxTypeList == null)
{
theTrxTypeList = new ArrayList<TransactionType>();
}
if(theDataElementList == null)
{
theDataElementList = new ArrayList<DataElement>();
}
}
@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException
{
if (localName.equalsIgnoreCase(TRX_TYPE))
{
this.currentTrxType = new TransactionType(attributes.getValue(NAME),
attributes.getValue(VALUE),attributes.getValue(AMOUNT),
attributes.getValue(T_TYPE),attributes.getValue(CLRK_ID),
attributes.getValue(INV_NUM), attributes.getValue(AUTH),
attributes.getValue(ORIG_SEQ), attributes.getValue(ORIG_REF));
this.theTrxTypeList.add(currentTrxType);
}
else if (localName.equalsIgnoreCase(TENDER_TYPE))
{
this.currentTenderType = new TenderType(attributes.getValue(NAME),
attributes.getValue(VALUE));
theTenderTypeList.add(currentTenderType);
}
else if (localName.equalsIgnoreCase(DATA_ELEM))
{
this.currentDataElement = new DataElement(attributes.getValue(TAG),
attributes.getValue(DESCRIPTION));
theDataElementList.add(currentDataElement);
}
}
public ArrayList<TenderType> getTenderTypeList()
{
return theTenderTypeList;
}
public ArrayList<TransactionType> getTrxTypeList()
{
return theTrxTypeList;
}
public ArrayList<DataElement> getDataElementList()
{
return theDataElementList;
}
}
我解决了!XML文件结构不好!在我改变它之后!成功了。感谢您的支持put带有变量的完整java文件
SAXParserFactory fabrique = SAXParserFactory.newInstance();
SAXParser parseur = null;
try
{
parseur = fabrique.newSAXParser();
}
catch (ParserConfigurationException e)
{
System.out.println("Parse Config Exception");
}
catch (SAXException e)
{
System.out.println("SAX Parse Exception");
}
handler = new XMLParser();
try
{
/* Parse Config File */
myFileCfg = new File(FolderPath + CFG_FILE_NAME);
parseur.parse(myFileCfg, handler);
}
catch (SAXException e)