[英]How to get element's value from XML using SAX parser in startElement?
是否可以從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
獲取屬性。 您應該以characters
獲得的值。
以下是如何使用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
類,每次在startElement
找到<employee>
時實例化它們,填充它屬性,並將其添加到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, String localName, String qName,
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 localName, 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, start, length);
}
if (isLocation) {
currentEmployee.location = new String(ch, start, 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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.