gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:58:49 26 4
gpt4 key购买 nike

是否可以在 startElement 函数(SAX 处理程序的覆盖函数)中从 XML 文件中获取元素的内容?

下面是规范。

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
}
}
}

如果你有一个简单的例子,为每个标签设置 boolean 标志是可以的。如果您有更复杂的场景,您可能更喜欢使用元素名称作为键将标志存储在 map 中,甚至创建一个或多个 Employee映射到您的 XML 的类,每次都实例化它们 <employee>startElement 中找到,填充其属性,并将其添加到 endElement 中的集合中.

这是一个完整的 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);
}
}
}

关于java - 如何在 startElement 中使用 SAX 解析器从 XML 中获取元素的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24113529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com