如何使用 SAX 在 1 class 处一次解析 2 xml 个文件
How to parse 2 xml files at 1 time at 1 class with SAX
我如何 "force" 解析器方法来使用第二个文件并制作第二个地图?
我对 1 个文件进行了 xml 解析和映射 (LinkedHashMap)。
我怎样才能对其他文件做同样的事情?
在我的代码中,您可以找到 2 个解析器实例、2 个 linkedHashMap。
但是方法只适用于 1 个文件,因为我不知道如何指向将其他文件用于方法。
到目前为止我的代码是:
package xml_strings;
import java.util.LinkedHashMap;
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 ReadingResourcesWithSax {
static LinkedHashMap<String, String> mapaEn = new LinkedHashMap<String, String>();
static LinkedHashMap<String, String> mapaPl = new LinkedHashMap<String, String>();
public static void main(String[] args) throws Exception {
XmlHandler xmlHandler = new XmlHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse("strings1.xml", xmlHandler);
SAXParser saxParser1 = factory.newSAXParser();
saxParser1.parse("strings2.xml", xmlHandler);
for (String key : mapaEn.keySet()) {
System.out.print(key + "\n");
System.out.println("<" + mapaEn.get(key) + ">\n");
}
}
private static class XmlHandler extends DefaultHandler {
private boolean insideStringItem;
private String a, b, c;
private int i = 0;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (qName.equals("string")) {
insideStringItem = true;
a = attributes.getValue("name");
}
else if (qName.equals("string-array")) {
insideStringItem = true;
a = attributes.getValue("name");
c = a;
} else if (qName.equals("item")) {
insideStringItem = true;
a = c + "###" + i;
i++;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (insideStringItem) {
insideStringItem = false;
b = new String(ch, start, length);
mapaEn.put(a, b);
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("string-array")) {
i = 0;
}
}
}
}
好的,我想通了,也许其他人会寻找该解决方案
使用相同的处理程序和相同的方法一次性解析多个文件:)
package xml_strings;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
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 ReadingResourcesWithSax {
static Map<String, String> mapaEn = new LinkedHashMap<String, String>();
static Map<String, String> mapaPl = new LinkedHashMap<String, String>();
public static void main(String[] args) throws Exception {
XmlHandler xmlHandler = new XmlHandler();
xmlHandler.setMap(mapaEn);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse("stringsEn.xml", xmlHandler);
XmlHandler xmlHandler1 = new XmlHandler();
xmlHandler1.setMap(mapaPl);
SAXParserFactory factory1 = SAXParserFactory.newInstance();
SAXParser saxParser1 = factory1.newSAXParser();
saxParser1.parse("stringsPl.xml", xmlHandler1);
for (String key : mapaEn.keySet()) {
System.out.print(key + "\n");
System.out.println("<" + mapaEn.get(key) + ">\n");
}
for (String key : mapaPl.keySet()) {
System.out.print(key + "\n");
System.out.println("<" + mapaPl.get(key) + ">\n");
}
List<Map<String, String>> listaMap = new ArrayList<Map<String, String>>();
listaMap.add(mapaEn);
listaMap.add(mapaPl);
Set<String> keySet = listaMap.get(0).keySet();
for(String key: keySet)
{
System.out.println(key);
for(Map<String, String> map: listaMap)
{
System.out.println("\t"+map.get(key));
}
}
for(Map<String, String> lista : listaMap) {
System.out.println("Wartosci dla tego samego klucza : " + lista.get("type_not_saved"));
}
}
private static class XmlHandler extends DefaultHandler {
private Map<String, String> map;
private boolean insideStringItem;
private String a, b, c;
private int i = 0;
public void setMap(Map<String, String> map)
{
this.map = map;
}
public Map<String, String> getMap()
{
return map;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (qName.equals("string")) {
insideStringItem = true;
a = attributes.getValue("name");
}
else if (qName.equals("string-array")) {
insideStringItem = true;
a = attributes.getValue("name");
map.put(a,"string-array");
c = a;
} else if (qName.equals("item")) {
insideStringItem = true;
a = c + "###" + i;
i++;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (insideStringItem) {
insideStringItem = false;
b = new String(ch, start, length);
map.put(a, b);
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("string-array")) {
i = 0;
}
}
}
}
我如何 "force" 解析器方法来使用第二个文件并制作第二个地图?
我对 1 个文件进行了 xml 解析和映射 (LinkedHashMap)。
我怎样才能对其他文件做同样的事情?
在我的代码中,您可以找到 2 个解析器实例、2 个 linkedHashMap。
但是方法只适用于 1 个文件,因为我不知道如何指向将其他文件用于方法。
到目前为止我的代码是:
package xml_strings;
import java.util.LinkedHashMap;
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 ReadingResourcesWithSax {
static LinkedHashMap<String, String> mapaEn = new LinkedHashMap<String, String>();
static LinkedHashMap<String, String> mapaPl = new LinkedHashMap<String, String>();
public static void main(String[] args) throws Exception {
XmlHandler xmlHandler = new XmlHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse("strings1.xml", xmlHandler);
SAXParser saxParser1 = factory.newSAXParser();
saxParser1.parse("strings2.xml", xmlHandler);
for (String key : mapaEn.keySet()) {
System.out.print(key + "\n");
System.out.println("<" + mapaEn.get(key) + ">\n");
}
}
private static class XmlHandler extends DefaultHandler {
private boolean insideStringItem;
private String a, b, c;
private int i = 0;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (qName.equals("string")) {
insideStringItem = true;
a = attributes.getValue("name");
}
else if (qName.equals("string-array")) {
insideStringItem = true;
a = attributes.getValue("name");
c = a;
} else if (qName.equals("item")) {
insideStringItem = true;
a = c + "###" + i;
i++;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (insideStringItem) {
insideStringItem = false;
b = new String(ch, start, length);
mapaEn.put(a, b);
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("string-array")) {
i = 0;
}
}
}
}
好的,我想通了,也许其他人会寻找该解决方案
使用相同的处理程序和相同的方法一次性解析多个文件:)
package xml_strings;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
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 ReadingResourcesWithSax {
static Map<String, String> mapaEn = new LinkedHashMap<String, String>();
static Map<String, String> mapaPl = new LinkedHashMap<String, String>();
public static void main(String[] args) throws Exception {
XmlHandler xmlHandler = new XmlHandler();
xmlHandler.setMap(mapaEn);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse("stringsEn.xml", xmlHandler);
XmlHandler xmlHandler1 = new XmlHandler();
xmlHandler1.setMap(mapaPl);
SAXParserFactory factory1 = SAXParserFactory.newInstance();
SAXParser saxParser1 = factory1.newSAXParser();
saxParser1.parse("stringsPl.xml", xmlHandler1);
for (String key : mapaEn.keySet()) {
System.out.print(key + "\n");
System.out.println("<" + mapaEn.get(key) + ">\n");
}
for (String key : mapaPl.keySet()) {
System.out.print(key + "\n");
System.out.println("<" + mapaPl.get(key) + ">\n");
}
List<Map<String, String>> listaMap = new ArrayList<Map<String, String>>();
listaMap.add(mapaEn);
listaMap.add(mapaPl);
Set<String> keySet = listaMap.get(0).keySet();
for(String key: keySet)
{
System.out.println(key);
for(Map<String, String> map: listaMap)
{
System.out.println("\t"+map.get(key));
}
}
for(Map<String, String> lista : listaMap) {
System.out.println("Wartosci dla tego samego klucza : " + lista.get("type_not_saved"));
}
}
private static class XmlHandler extends DefaultHandler {
private Map<String, String> map;
private boolean insideStringItem;
private String a, b, c;
private int i = 0;
public void setMap(Map<String, String> map)
{
this.map = map;
}
public Map<String, String> getMap()
{
return map;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (qName.equals("string")) {
insideStringItem = true;
a = attributes.getValue("name");
}
else if (qName.equals("string-array")) {
insideStringItem = true;
a = attributes.getValue("name");
map.put(a,"string-array");
c = a;
} else if (qName.equals("item")) {
insideStringItem = true;
a = c + "###" + i;
i++;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (insideStringItem) {
insideStringItem = false;
b = new String(ch, start, length);
map.put(a, b);
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("string-array")) {
i = 0;
}
}
}
}