如何在android上解析xml文件

发布于 2024-12-26 09:53:35 字数 208 浏览 1 评论 0原文

android 有一些 dom/sax 解析器吗?

例如 xml 文件:

<A>
   <B>
   </B>
   <C>
   </C>
</A>

我的文件更复杂,但这是一个简单的示例。
android 有 dom/sax 解析器吗?

Does android has some dom/sax parsers?

E.g. of xml file:

<A>
   <B>
   </B>
   <C>
   </C>
</A>

My file is more complex but i this was a simple example.
Does android have some dom/sax parser?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸印温柔 2025-01-02 09:53:35

是的,Android也支持SAXParser,并且代码与普通java程序没有区别

SAX 解析器使用回调函数(org.xml.sax.helpers.DefaultHandler)来通知客户端 XML 文档结构。您应该扩展 DefaultHandler 并重写一些方法来实现 xml 解析。
要重写的方法是

  • startDocument() 和 endDocument() – 在 XML 文档的开头和结尾调用的方法。
  • startElement() 和 endElement() – 在文档元素的开头和结尾调用的方法。
  • strings() – 使用 XML 文档元素的开始标签和结束标签之间的文本内容调用的方法。

对于工作代码,请访问:
使用 SaxParser 进行 XML 解析并附完整代码

Yes, Android also supports SAXParser, and the code will not differ from a normal java program.

SAX parser use callback function (org.xml.sax.helpers.DefaultHandler) to informs clients of the XML document structure. You should extend DefaultHandler and override few methods to achieve xml parsing.
The methods to override are

  • startDocument() and endDocument() – Method called at the start and end of an XML document.
  • startElement() and endElement() – Method called at the start and end of a document element.
  • characters() – Method called with the text contents in between the start and end tags of an XML document element.

For a working code visit:
XML parsing using SaxParser with complete code

一笑百媚生 2025-01-02 09:53:35

[Android Xml解析教程][1]

         we can parse xml files in android is very simple. In market there are many xml parsers are available to parse xml data in android. But simplexml is one of the best xml parser in android.
  1. 创建一个Android应用程序项目。
  2. 从互联网下载 simple-xml.jar。
  3. 准备您的 xml 文件。

Employee.xml

<Employees>
   <Employee>
     <id>01</id>
     <name>jagadeesh</name>
     <salary>00000</salary>
   </Employee>
   <Employee>
     <id>02</id>
     <name>jaggubai</name>
     <salary>00000</salary>
   </Employee>
   <Employee>
     <id>03</id>
     <name>jaggudada</name>
     <salary>00000</salary>
   </Employee>
<Employees>
  1. 现在您需要创建两个 pojo 类。因为这里我们使用两种东西,一种是员工群体,另一种是个体员工。因此,对于员工组,我们需要创建Employees.java,对于单个员工,我们需要创建Employee.java。

员工.java

public class Employee{
    @Element
    public String id;
    @Element
    public String name;
    @Element
    public String salary;
}

员工.java

@Root
public class Employees{
    @ElementList(inline=true, entry="Employee")
    public List<Employee> listOfEmployees;
}

[Android Xml Parsing Tutorial][1]

         we can parse xml files in android is very simple. In market there are many xml parsers are available to parse xml data in android. But simplexml is one of the best xml parser in android.
  1. Create one Android Application Project.
  2. Download simple-xml.jar from internet.
  3. Prepare your xml file.

Employee.xml

<Employees>
   <Employee>
     <id>01</id>
     <name>jagadeesh</name>
     <salary>00000</salary>
   </Employee>
   <Employee>
     <id>02</id>
     <name>jaggubai</name>
     <salary>00000</salary>
   </Employee>
   <Employee>
     <id>03</id>
     <name>jaggudada</name>
     <salary>00000</salary>
   </Employee>
<Employees>
  1. Now you need to create two pojo classes. Because here we are using two things one is group of employees and another is individual employees. So, for group of employees we need to create Employees.java and for individual employees Employee.java.

Employee.java

public class Employee{
    @Element
    public String id;
    @Element
    public String name;
    @Element
    public String salary;
}

Employees.java

@Root
public class Employees{
    @ElementList(inline=true, entry="Employee")
    public List<Employee> listOfEmployees;
}
苏别ゝ 2025-01-02 09:53:35

public class SAXParserExample extends DefaultHandler{

List myEmpls;

private String tempVal;

//to maintain context
private Employee tempEmp;


public SAXParserExample(){
    myEmpls = new ArrayList();
}

public void runExample() {
    parseDocument();
    printData();
}

private void parseDocument() {

    //get a factory
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {

        //get a new instance of parser
        SAXParser sp = spf.newSAXParser();

        //parse the file and also register this class for call backs
        sp.parse("employees.xml", this);

    }catch(SAXException se) {
        se.printStackTrace();
    }catch(ParserConfigurationException pce) {
        pce.printStackTrace();
    }catch (IOException ie) {
        ie.printStackTrace();
    }
}

/**
 * Iterate through the list and print
 * the contents
 */
private void printData(){

    System.out.println("No of Employees '" + myEmpls.size() + "'.");

    Iterator it = myEmpls.iterator();
    while(it.hasNext()) {
        System.out.println(it.next().toString());
    }
}


//Event Handlers
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    //reset
    tempVal = "";
    if(qName.equalsIgnoreCase("Employee")) {
        //create a new instance of employee
        tempEmp = new Employee();
        tempEmp.setType(attributes.getValue("type"));
    }
}


public void characters(char[] ch, int start, int length) throws SAXException {
    tempVal = new String(ch,start,length);
}

public void endElement(String uri, String localName, String qName) throws SAXException {

    if(qName.equalsIgnoreCase("Employee")) {
        //add it to the list
        myEmpls.add(tempEmp);

    }else if (qName.equalsIgnoreCase("Name")) {
        tempEmp.setName(tempVal);
    }else if (qName.equalsIgnoreCase("Id")) {
        tempEmp.setId(Integer.parseInt(tempVal));
    }else if (qName.equalsIgnoreCase("Age")) {
        tempEmp.setAge(Integer.parseInt(tempVal));
    }

}

public static void main(String[] args){
    SAXParserExample spe = new SAXParserExample();
    spe.runExample();
}

}

对于 xml
;
<员工类型=“永久”>
<名称>海鸥
3674
<年龄>34

<员工类型=“合同”>
<姓名>罗宾
3675
<年龄>25

<员工类型=“永久”>
<名称>乌鸦
3676
<年龄>28

检查此链接:http://www.java-samples.com/showtutorial.php?tutorialid=152" java-samples.com/showtutorial.php?tutorialid=152

public class SAXParserExample extends DefaultHandler{

List myEmpls;

private String tempVal;

//to maintain context
private Employee tempEmp;


public SAXParserExample(){
    myEmpls = new ArrayList();
}

public void runExample() {
    parseDocument();
    printData();
}

private void parseDocument() {

    //get a factory
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {

        //get a new instance of parser
        SAXParser sp = spf.newSAXParser();

        //parse the file and also register this class for call backs
        sp.parse("employees.xml", this);

    }catch(SAXException se) {
        se.printStackTrace();
    }catch(ParserConfigurationException pce) {
        pce.printStackTrace();
    }catch (IOException ie) {
        ie.printStackTrace();
    }
}

/**
 * Iterate through the list and print
 * the contents
 */
private void printData(){

    System.out.println("No of Employees '" + myEmpls.size() + "'.");

    Iterator it = myEmpls.iterator();
    while(it.hasNext()) {
        System.out.println(it.next().toString());
    }
}


//Event Handlers
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    //reset
    tempVal = "";
    if(qName.equalsIgnoreCase("Employee")) {
        //create a new instance of employee
        tempEmp = new Employee();
        tempEmp.setType(attributes.getValue("type"));
    }
}


public void characters(char[] ch, int start, int length) throws SAXException {
    tempVal = new String(ch,start,length);
}

public void endElement(String uri, String localName, String qName) throws SAXException {

    if(qName.equalsIgnoreCase("Employee")) {
        //add it to the list
        myEmpls.add(tempEmp);

    }else if (qName.equalsIgnoreCase("Name")) {
        tempEmp.setName(tempVal);
    }else if (qName.equalsIgnoreCase("Id")) {
        tempEmp.setId(Integer.parseInt(tempVal));
    }else if (qName.equalsIgnoreCase("Age")) {
        tempEmp.setAge(Integer.parseInt(tempVal));
    }

}

public static void main(String[] args){
    SAXParserExample spe = new SAXParserExample();
    spe.runExample();
}

}

for xml
<Personnel>
<Employee type="permanent">
<Name>Seagull</Name>
<Id>3674</Id>
<Age>34</Age>
</Employee>
<Employee type="contract">
<Name>Robin</Name>
<Id>3675</Id>
<Age>25</Age>
</Employee>
<Employee type="permanent">
<Name>Crow</Name>
<Id>3676</Id>
<Age>28</Age>
</Employee>
</Personnel>

Check this link : http://www.java-samples.com/showtutorial.php?tutorialid=152

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文