XmlParserUtils.java这个文件在哪下载
时间: 2024-05-01 15:23:39 浏览: 90
XmlParserUtils.java这个文件并不是一个独立的程序,而是一个Java类文件,可以在你的Java项目中使用。你可以在网上搜索XmlParserUtils.java并下载它,或者自己手动创建这个类文件。以下是一个简单的Xml解析器示例代码,你可以参考一下:
```
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;
public class XmlParserUtils {
public static void parse(String filePath) {
try {
File inputFile = new File(filePath);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean bFirstName = false;
boolean bLastName = false;
boolean bNickName = false;
boolean bMarks = false;
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.println("Start Element: " + qName);
if (qName.equalsIgnoreCase("STUDENT")) {
String rollNo = attributes.getValue("rollno");
System.out.println("Roll No : " + rollNo);
} else if (qName.equalsIgnoreCase("FIRSTNAME")) {
bFirstName = true;
} else if (qName.equalsIgnoreCase("LASTNAME")) {
bLastName = true;
} else if (qName.equalsIgnoreCase("NICKNAME")) {
bNickName = true;
}
else if (qName.equalsIgnoreCase("MARKS")) {
bMarks = true;
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.println("End Element: " + qName);
}
public void characters(char ch[], int start, int length) throws SAXException {
if (bFirstName) {
System.out.println("First Name: " + new String(ch, start, length));
bFirstName = false;
} else if (bLastName) {
System.out.println("Last Name: " + new String(ch, start, length));
bLastName = false;
} else if (bNickName) {
System.out.println("Nick Name: " + new String(ch, start, length));
bNickName = false;
} else if (bMarks) {
System.out.println("Marks: " + new String(ch, start, length));
bMarks = false;
}
}
};
saxParser.parse(inputFile, handler);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
parse("students.xml");
}
}
```
这个示例代码可以解析一个名为students.xml的XML文件,并输出其中的学生信息。你可以根据自己的需求修改代码。
阅读全文