Java 如何在 startElement 中使用 SAX 解析器从 XML 获取元素的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24113529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get element's value from XML using SAX parser in startElement?
提问by sakura
Is it possible to get the content of an element from a XML file in startElement
function that is the override function of the SAX handler?
是否可以从作为startElement
SAX 处理程序的覆盖函数的函数中的XML 文件中获取元素的内容?
Below is the specification.
下面是规格。
1) XML file
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
function
2)startElement
功能
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
.......code in here..........
}
3) Expected result
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
回答by helderdarocha
You can get the element's namein startElement
and endElement
. You can also get attributes in startElement
. Values you should get in characters
.
您可以在和 中获取元素的名称。您还可以在. 你应该得到的价值观。startElement
endElement
startElement
characters
Here is a very basic exampleon how to get the value of an element using a ContentHandler
:
这是一个非常基本的示例,说明如何使用 a 获取元素的值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
}
}
}
If you have a simple example, setting boolean flags for each tag is OK. If you have a more complex scenario, you might prefer store the flags in a map using element names as keys, or even create one or more Employee
classes mapped to your XML, instantiate them every time <employee>
is found in startElement
, populate its properties, and add it to a Collection in endElement
.
如果你有一个简单的例子,为每个标签设置布尔标志就可以了。如果您有更复杂的场景,您可能更喜欢使用元素名称作为键将标志存储在映射中,甚至创建一个或多个Employee
映射到您的 XML 的类,每次<employee>
在 中找到时实例化它们startElement
,填充其属性,然后添加它到endElement
.
Here is a complete ContentHandler
example that works with your example file. I hope it helps you get started:
这是一个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);
}
}
}