加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在startElement中使用SAX解析器从XML获取元素的值?

发布时间:2020-12-16 23:50:42 所属栏目:百科 来源:网络整理
导读:是否可以从startElement函数中的 XML文件中获取元素的内容,该函数是SAX处理程序的覆盖函数? 以下是规格. 1)XML文件 employees employee id="111" firstNameRakesh/firstName lastNameMishra/lastName locationBangalore/location /employee employee id="11
是否可以从startElement函数中的 XML文件中获取元素的内容,该函数是SAX处理程序的覆盖函数?

以下是规格.

1)XML文件

<employees>
   <employee id="111">
      <firstName>Rakesh</firstName>
      <lastName>Mishra</lastName>
      <location>Bangalore</location>
   </employee>
   <employee id="112">
      <firstName>John</firstName>
      <lastName>Davis</lastName>
      <location>Chennai</location>
   </employee>
   <employee id="113">
      <firstName>Rajesh</firstName>
      <lastName>Sharma</lastName>
      <location>Pune</location>
   </employee>
</employees>

2)startElement函数

@Override
public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException {
    .......code in here..........
}

3)预期结果

element name   : employee
attribute name : id
attribute value: 111
firstName      : Rakesh
lastName       : Mishra
location       : Bangalore

element name   : employee
attribute name : id
attribute value: 112
firstName      : John
lastName       : Davis
location       : Chennai

element name   : employee
attribute name : id
attribute value: 113
firstName      : Rajesh
lastName       : Sharma
location       : Pune
您可以在startElement和endElement中获取元素的名称.您还可以在startElement中获取属性.您应该以字符形式获得的值.

以下是如何使用ContentHandler获取元素值的一个非常基本的示例:

public class YourHandler extends DefaultHandler {

    boolean inFirstNameElement = false;

    public class startElement(....) {
        if(qName.equals("firstName") {
            inFirstNameElement = true;
        }
    }

    public class endElement(....) {
        if(qName.equals("firstName") {
            inFirstNameElement = false;
        }
    }

    public class characters(....) {
        if(inFirstNameElement) {
            // do something with the characters in the <firstName> element
        }
    }
}

如果您有一个简单的示例,则为每个标记设置布尔标记即可.如果您有一个更复杂的场景,您可能更喜欢使用元素名称作为键将标志存储在映射中,或者甚至创建一个或多个映射到XML的Employee类,并在每次< employee>时实例化它们.在startElement中找到,填充其属性,并将其添加到endElement中的Collection.

这是一个完整的ContentHandler示例,适用于您的示例文件.我希望它可以帮助你开始:

public class SimpleHandler extends DefaultHandler {

    class Employee {
        public String firstName;
        public String lastName;
        public String location;
        public Map<String,String> attributes = new HashMap<>();
    }
    boolean isFirstName,isLastName,isLocation;
    Employee currentEmployee;
    List<Employee> employees = new ArrayList<>();

    @Override
    public void startElement(String uri,Attributes atts) throws SAXException {
        if(qName.equals("employee")) {
            currentEmployee = new Employee();
            for(int i = 0; i < atts.getLength(); i++) {
                currentEmployee.attributes.put(atts.getQName(i),atts.getValue(i));
            }
        }
        if(qName.equals("firstName")) { isFirstName = true; }
        if(qName.equals("lastName"))  { isLastName = true;  }
        if(qName.equals("location"))  { isLocation = true;  }
    }

    @Override
    public void endElement(String uri,String qName)
            throws SAXException {
        if(qName.equals("employee")) {
            employees.add(currentEmployee);
            currentEmployee = null;
        }
        if(qName.equals("firstName")) { isFirstName = false; }
        if(qName.equals("lastName"))  { isLastName = false;  }
        if(qName.equals("location"))  { isLocation = false;  }
    }

    @Override
    public void characters(char[] ch,int start,int length) throws SAXException {
        if (isFirstName) {
            currentEmployee.firstName = new String(ch,start,length);
        }
        if (isLastName) {
            currentEmployee.lastName = new String(ch,length);
        }
        if (isLocation) {
            currentEmployee.location = new String(ch,length);
        }
    }

    @Override
    public void endDocument() throws SAXException {
        for(Employee e: employees) {
            System.out.println("Employee ID: " + e.attributes.get("id"));
            System.out.println("  First Name: " + e.firstName);
            System.out.println("  Last Name: " + e.lastName);
            System.out.println("  Location: " + e.location);
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读