读取条件年龄 >30 的 XML 文件并在 Java 控制台中输出
Read XML File with condition age >30 and output in Java Console
我正在 Eclipse 中读取 XML 文件,我的输出在我的控制台中。
到目前为止,我设法输出了我的条目。
但是我需要打印我的员工超过30岁的条目。
这是我的 XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<company>
<name>CompanyName</name>
<employees id="0">
<name>employee name0</name>
<age>33</age>
<role>tester</role>
<gen>male</gen>
</employees>
<employees id="1">
<name>employee name1</name>
<age>18</age>
<role>tester</role>
<gen>female</gen>
</employees>
<employees id="2">
<name>employee name2</name>
<age>38</age>
<role>developer</role>
<gen>male</gen>
</employees>
</company>
这就是我一直在尝试的:
if (qName.equals("age"))
{
int age2;
String age=attributes.getValue("age");
age2=Integer.ParseInt(age)
if (age2>30){
System.out.println("\tAge="+age2);
}
所以我想打印下来
id=0 的员工和 id=2 的员工,因为他们的年龄 >30
考虑到您正在使用 SAXParser 并且此代码片段位于重写方法 startElement
内,您还需要重写 characters
和 endElement
。像这样:
class Handler extends DefaultHandler {
String currentElement;
String currentAgeValue;
String currentNameValue;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
currentElement = qName;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
switch(currentElement) {
case "age":
currentAgeValue = new String(ch, start, length);
break;
case "name":
currentNameValue = new String(ch, start, length);
break;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
if(qName.equals("employees")) {
int age = Integer.parseInt(currentAgeValue);
if(age > 30) {
System.out.println("Name:" + currentNameValue+", Age:" + age);
}
}
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
String xml = "<company><name>CompanyName</name><employees id=\"0\"><name>employee name0</name><age>33</age><role>tester</role><gen>male</gen></employees><employees id=\"1\"><name>employee name1</name><age>18</age><role>tester</role><gen>female</gen></employees><employees id=\"2\"><name>employee name2</name><age>38</age><role>developer</role><gen>male</gen></employees></company>";
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new InputSource(new StringReader(xml)), new Handler());
}
输出将是:
Name:employee name0, Age:33
Name:employee name2, Age:38
读取给定元素的值时调用characters
方法,startElement
的Attribute参数保留XML属性的值,如id 在 <employees id="2">
.
我正在 Eclipse 中读取 XML 文件,我的输出在我的控制台中。 到目前为止,我设法输出了我的条目。 但是我需要打印我的员工超过30岁的条目。
这是我的 XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<company>
<name>CompanyName</name>
<employees id="0">
<name>employee name0</name>
<age>33</age>
<role>tester</role>
<gen>male</gen>
</employees>
<employees id="1">
<name>employee name1</name>
<age>18</age>
<role>tester</role>
<gen>female</gen>
</employees>
<employees id="2">
<name>employee name2</name>
<age>38</age>
<role>developer</role>
<gen>male</gen>
</employees>
</company>
这就是我一直在尝试的:
if (qName.equals("age"))
{
int age2;
String age=attributes.getValue("age");
age2=Integer.ParseInt(age)
if (age2>30){
System.out.println("\tAge="+age2);
}
所以我想打印下来
id=0 的员工和 id=2 的员工,因为他们的年龄 >30
考虑到您正在使用 SAXParser 并且此代码片段位于重写方法 startElement
内,您还需要重写 characters
和 endElement
。像这样:
class Handler extends DefaultHandler {
String currentElement;
String currentAgeValue;
String currentNameValue;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
currentElement = qName;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
switch(currentElement) {
case "age":
currentAgeValue = new String(ch, start, length);
break;
case "name":
currentNameValue = new String(ch, start, length);
break;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
if(qName.equals("employees")) {
int age = Integer.parseInt(currentAgeValue);
if(age > 30) {
System.out.println("Name:" + currentNameValue+", Age:" + age);
}
}
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
String xml = "<company><name>CompanyName</name><employees id=\"0\"><name>employee name0</name><age>33</age><role>tester</role><gen>male</gen></employees><employees id=\"1\"><name>employee name1</name><age>18</age><role>tester</role><gen>female</gen></employees><employees id=\"2\"><name>employee name2</name><age>38</age><role>developer</role><gen>male</gen></employees></company>";
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new InputSource(new StringReader(xml)), new Handler());
}
输出将是:
Name:employee name0, Age:33
Name:employee name2, Age:38
读取给定元素的值时调用characters
方法,startElement
的Attribute参数保留XML属性的值,如id 在 <employees id="2">
.