Bất kỳ ai có thể cho tôi biết cách phân tích cú pháp tệp XML cục bộ được lưu trữ trong hệ thống bằng SAX, với mã ví dụ? Xin vui lòng cho tôi biết nơi tôi có thể tìm thấy thông tin về điều đó.Phân tích cú pháp tệp XML cục bộ bằng cách sử dụng Sax trong Android
Trả lời
Để đọc từ XML trong ứng dụng của bạn, hãy tạo thư mục trong thư mục res bên trong dự án của bạn có tên "xml" (chữ thường). Lưu trữ xml của bạn trong thư mục mới được tạo này. Để nạp XML từ các nguồn tài nguyên của bạn,
XmlResourceParser myxml = mContext.getResources().getXml(R.xml.MyXml);//MyXml.xml is name of our xml in newly created xml folder, mContext is the current context
// Alternatively use: XmlResourceParser myxml = getContext().getResources().getXml(R.xml.MyXml);
myxml.next();//Get next parse event
int eventType = myxml.getEventType(); //Get current xml event i.e., START_DOCUMENT etc.
Sau đó bạn có thể bắt đầu xử lý các nút, các thuộc tính vv và văn bản chứa trong vỏ của các loại sự kiện, một khi xử lý cuộc gọi myxml.next() để có được những sự kiện tiếp theo, tức là ,
String NodeValue;
while (eventType != XmlPullParser.END_DOCUMENT) //Keep going until end of xml document
{
if(eventType == XmlPullParser.START_DOCUMENT)
{
//Start of XML, can check this with myxml.getName() in Log, see if your xml has read successfully
}
else if(eventType == XmlPullParser.START_TAG)
{
NodeValue = myxml.getName();//Start of a Node
if (NodeValue.equalsIgnoreCase("FirstNodeNameType"))
{
// use myxml.getAttributeValue(x); where x is the number
// of the attribute whose data you want to use for this node
}
if (NodeValue.equalsIgnoreCase("SecondNodeNameType"))
{
// use myxml.getAttributeValue(x); where x is the number
// of the attribute whose data you want to use for this node
}
//etc for each node name
}
else if(eventType == XmlPullParser.END_TAG)
{
//End of document
}
else if(eventType == XmlPullParser.TEXT)
{
//Any XML text
}
eventType = myxml.next(); //Get next event from xml parser
}
package com.xml; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import android.util.Log; import android.widget.Toast; public class FeedHandler extends DefaultHandler { StringBuilder sb = null; String ret = ""; boolean bStore = false; int howMany = 0; FeedHandler() { } String getResults() { return "XML parsed data.\nThere are [" + howMany + "] status updates\n\n" + ret; } @Override public void startDocument() throws SAXException { // initialize "list" } @Override public void endDocument() throws SAXException { } @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { try { if (localName.equals("status")) { this.sb = new StringBuilder(""); bStore = true; } if (localName.equals("user")) { bStore = false; } if (localName.equals("text")) { this.sb = new StringBuilder(""); Log.i("sb ", sb+""); } if (localName.equals("created_at")) { this.sb = new StringBuilder(""); } } catch (Exception e) { Log.d("error in startElement", e.getStackTrace().toString()); } } @Override public void endElement(String namespaceURI, String localName, String qName) throws SAXException { if (bStore) { if (localName.equals("created_at")) { ret += "Date: " + sb.toString() + "\n"; sb = new StringBuilder(""); return; } if (localName.equals("user")) { bStore = true; } if (localName.equals("text")) { ret += "Post: " + sb.toString() + "\n\n"; sb = new StringBuilder(""); return; } } if (localName.equals("status")) { howMany++; bStore = false; } } @Override public void characters(char ch[], int start, int length) { if (bStore) { // System.out.println("start " +start+"length "+length); String theString = new String(ch, start, length); this.sb.append(theString+"start "+start+" length "+length); } } }
InputSource is = new InputSource(getResources().openRawResource(R.raw.my)); System.out.println("running xml file..... "); // create the factory SAXParserFactory factory = SAXParserFactory.newInstance(); // create a parser SAXParser parser = factory.newSAXParser(); // create the reader (scanner) XMLReader xmlreader = parser.getXMLReader(); // instantiate our handler FeedHandler fh = new FeedHandler(); // assign our handler xmlreader.setContentHandler(fh); // perform the synchronous parse xmlreader.parse(is); // should be done... let's display our results tvData.setText(fh.getResults());
mẫu mã
Tạo tài liệuBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- Tạo DocumentBuilder
DocumentBuilder builder = nhà máy. newDocumentBuilder();
- nhận luồng đầu vào ClassLoader cls = DomReader.class.getClassLoader(); InputStream là = cls.getResourceAsStream ("tệp xml");
- phân tích cú pháp tệp xml và nhận đối tượng Tài liệu bằng cách gọi phương thức phân tích cú pháp trên đối tượng DocumentBuilder. Tài liệu tài liệu = builder.parse (is);
- Traverse cây dom bằng đối tượng tài liệu. SAX: Phân tích xml đơn giản. Nó phân tách nút bằng nút Traversing là từ trên xuống dưới Sử dụng bộ nhớ thấp Điều hướng quay lại không thể thực hiện được với sax.
// thực hiện xử lý yêu cầu public class SaxParse kéo dài DefaultHandler {} // thể hiện mới của saxParserFactory SAXParserFactory nhà máy = SAXParserFactory.newInstance(); // CƠ SỞ MỚI CỦA SAX PARSER SAXParser saxparser = factory.newSAXParser(); // Phân tích cú pháp tài liệu xml SAXParser.parse (tệp mới (tệp được phân tích cú pháp), SAXXMLParserImpl() mới;
- 1. Phân tích cú pháp tệp XML cục bộ trong Android
- 2. XML phân tích cú pháp - ElementTree vs SAX và DOM
- 3. Phân tích cú pháp XML bằng cách sử dụng XDocument
- 4. Phân tích html với trình phân tích cú pháp SAX
- 5. Sự khác biệt giữa trình phân tích cú pháp SAX XML, Kéo trình phân tích cú pháp & Trình phân tích cú pháp DOM trong android
- 6. "Lỗi khi phân tích cú pháp tệp XML" khi phân tích cú pháp bằng cách sử dụng XInclude
- 7. Sử dụng JavaScript để phân tích cú pháp tệp XML
- 8. Phân tích cú pháp XML bằng Python
- 9. Phân tích cú pháp XML với SAX trong java, phân biệt chữ hoa chữ thường.
- 10. cách phân tích dữ liệu xml bằng cách sử dụng phân tích cú pháp libxml
- 11. Cách phân tích cú pháp tệp XML trong RapidXML
- 12. Phân tích cú pháp tệp xml cục bộ trong iOS sdk
- 13. Phân tích cú pháp xml bằng powershell
- 14. C# Phân tích cú pháp tệp XML
- 15. Phân tích cú pháp XML Libxmljs (Node.js)
- 16. Perl, cách phân tích cú pháp tệp XML, xpath
- 17. Kịch bản Perl để phân tích cú pháp XML bằng cách sử dụng XML :: LibXML;
- 18. Phân tích cú pháp XML bằng cách sử dụng XSD trong PHP
- 19. Cách phân tích cú pháp XML (org.w3c.Document) trên Android
- 20. Phân tích cú pháp XML trong Cocoa
- 21. Phân tích cú pháp XML trong VBNet
- 22. Phân tích cú pháp XML PHP
- 23. Phân tích cú pháp html bằng cách sử dụng C
- 24. Phân tích cú pháp XML tiếng Ả Rập trong Android
- 25. Phân tích cú pháp các tệp XML lớn trong PHP
- 26. Phân tích cú pháp XML của Python
- 27. Phân tích cú pháp XML CDATA bằng PHP
- 28. Phân tích cú pháp tệp JSON cục bộ bằng jQuery và Javascript
- 29. Phân tích cú pháp XML trong iPhone
- 30. xml: phân tích cú pháp trong PHP