我在解析XML文件时遇到问题。它的结构如下:
<?xml version="1.0" encoding="UTF-8"?>
<teryt>
<catalog name="TERC" type="all" date="2014-01-01">
<row>
<col name="WOJ">02</col>
<col name="POW"/>
<col name="GMI"/>
<col name="RODZ"/>
<col name="NAZWA">DOLNOŚLĄSKIE</col>
<col name="NAZDOD">województwo</col>
<col name="STAN_NA">2014-01-01</col>
</row>
<row>
<col name="WOJ">02</col>
<col name="POW">01</col>
<col name="GMI"/>
<col name="RODZ"/>
<col name="NAZWA">bolesławiecki</col>
<col name="NAZDOD">powiat</col>
<col name="STAN_NA">2014-01-01</col>
</row>
...
</catalog
</teryt>
我现在有解析器,但它只读取一个变量的文件。我的解析器代码。
package pl.op.web.common;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.primefaces.event.FileUploadEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;
import java.io.InputStream;
@Name("op.xmlParser")
@Scope(ScopeType.SESSION)
public class XMLParser extends DefaultHandler {
private Logger log = LoggerFactory.getLogger(XMLParser.class);
private InputStream uploadedAreaFile;
public void uploadAreaXML(FileUploadEvent event) {
try {
uploadedAreaFile = event.getFile().getInputstream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getAreaXml() {
try {
SAXParserFactory saxParserFactory = SAXParserFactory
.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
DefaultHandler defaultHandler = new DefaultHandler() {
Boolean wojTag = false;
Boolean powTag = false;
Boolean gmiTag = false;
Boolean rodzTag = false;
Boolean nazwaTag = false;
Boolean nazdodTag = false;
Boolean stan_naTag = false;
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("col")) {
wojTag = true;
}
if (qName.equalsIgnoreCase("col")) {
powTag = true;
}
if (qName.equalsIgnoreCase("col")) {
gmiTag = true;
}
if (qName.equalsIgnoreCase("col")) {
rodzTag = true;
}
if (qName.equalsIgnoreCase("col")) {
nazwaTag = true;
}
if (qName.equalsIgnoreCase("col")) {
nazdodTag = true;
}
if (qName.equalsIgnoreCase("col")) {
stan_naTag = true;
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
if (wojTag.equals(true)) {
log.info("Woj : "
+ new String(ch, start, length));
}
if (powTag.equals(true)) {
log.info("Pow : "
+ new String(ch, start, length));
}
if (gmiTag.equals(true)) {
log.info("Gmi : "
+ new String(ch, start, length));
}
if (rodzTag.equals(true)) {
log.info("Rodz : "
+ new String(ch, start, length));
}
if (nazwaTag.equals(true)) {
log.info("Nazwa : "
+ new String(ch, start, length));
}
if (nazdodTag.equals(true)) {
log.info("Nazdod : "
+ new String(ch, start, length));
}
if (stan_naTag.equals(true)) {
log.info("Stan_na : "
+ new String(ch, start, length));
}
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
if (qName.equalsIgnoreCase("col")) {
wojTag = false;
}
if (qName.equalsIgnoreCase("col")) {
powTag = false;
}
if (qName.equalsIgnoreCase("col")) {
gmiTag = false;
}
if (qName.equalsIgnoreCase("col")) {
rodzTag = false;
}
if (qName.equalsIgnoreCase("col")) {
nazwaTag = false;
}
if (qName.equalsIgnoreCase("col")) {
nazdodTag = false;
}
if (qName.equalsIgnoreCase("col")) {
stan_naTag = false;
}
}
};
saxParser.parse(uploadedAreaFile, defaultHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如何在单个变量中包含每个属性(WOJ,POW,NAZWA等)?
答案 0 :(得分:0)
这是你可以做的......
包pl.op.web.common;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLParser extends DefaultHandler {
public static void main(String[] args) {
XMLParser parser = new XMLParser();
parser.getAreaXml();
}
public void getAreaXml() {
final List<StringBuilder> first = new ArrayList<StringBuilder>();
try {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
DefaultHandler defaultHandler = new DefaultHandler() {
Boolean wojTag = false;
Boolean powTag = false;
Boolean gmiTag = false;
Boolean rodzTag = false;
Boolean nazwaTag = false;
Boolean nazdodTag = false;
Boolean stan_naTag = false;
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("col")) {
if (attributes.getValue(0) != null)
if (attributes.getValue(0).equalsIgnoreCase("WOJ"))
wojTag = true;
if (attributes.getValue(0) != null)
if (attributes.getValue(0).equalsIgnoreCase("POW"))
powTag = true;
if (attributes.getValue(0) != null)
if (attributes.getValue(0).equalsIgnoreCase("GMI"))
gmiTag = true;
if (attributes.getValue(0) != null)
if (attributes.getValue(0).equalsIgnoreCase("RODZ"))
rodzTag = true;
if (attributes.getValue(0) != null)
if (attributes.getValue(0)
.equalsIgnoreCase("NAZWA"))
nazwaTag = true;
if (attributes.getValue(0) != null)
if (attributes.getValue(0).equalsIgnoreCase(
"NAZDOD"))
nazdodTag = true;
if (attributes.getValue(0) != null)
if (attributes.getValue(0).equalsIgnoreCase(
"STAN_NA"))
stan_naTag = true;
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
StringBuilder temp = new StringBuilder();
if (wojTag.equals(true)) {
temp.append(new String(ch, start, length));
temp.append(" ");
}
if (powTag.equals(true)) {
temp.append(new String(ch, start, length));
temp.append(" ");
}
if (gmiTag.equals(true)) {
temp.append(new String(ch, start, length));
temp.append(" ");
}
if (rodzTag.equals(true)) {
temp.append(new String(ch, start, length));
temp.append(" ");
}
if (nazwaTag.equals(true)) {
temp.append(new String(ch, start, length));
temp.append(" ");
}
if (nazdodTag.equals(true)) {
temp.append(new String(ch, start, length));
temp.append(" ");
}
if (stan_naTag.equals(true)) {
temp.append(new String(ch, start, length));
temp.append(" ");
}
first.add(temp);
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
if (qName.equalsIgnoreCase("col")) {
wojTag = false;
}
if (qName.equalsIgnoreCase("col")) {
powTag = false;
}
if (qName.equalsIgnoreCase("col")) {
gmiTag = false;
}
if (qName.equalsIgnoreCase("col")) {
rodzTag = false;
}
if (qName.equalsIgnoreCase("col")) {
nazwaTag = false;
}
if (qName.equalsIgnoreCase("col")) {
nazdodTag = false;
}
if (qName.equalsIgnoreCase("col")) {
stan_naTag = false;
}
}
};
saxParser.parse("E:/ResourcePDF/zippertest/src/test.xml",
defaultHandler);
for (StringBuilder s : first) {
System.out.print(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
对于上面的xml文件,out put将是这样的
02 DOLNO?L?SKIEwojewództwo2014-01-0102 01 boles?awiecki powiat 2014-01-01
将数据btw行标记绑定到一个StringBuilder中。并将其添加到“第一个”stringBuilder列表中。然后打印出来。