所以我開始使用 xml 和 SAX 解析器,現在我試圖弄清楚它是如何工作的,我熟悉 JSON,但這似乎不像 JSON 那樣工作。所以這是我正在使用的代碼package com.myalbion.gamedataextractor.handlers;import java.io.File;import java.io.IOException;import java.util.List;import java.util.Map;import javax.xml.parsers.ParserConfigurationException;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 com.myalbion.gamedataextractor.Main;import com.myalbion.gamedataextractor.datatables.Language;import com.myalbion.gamedataextractor.datatables.Localized;import com.myalbion.gamedataextractor.datatables.XMLFile;public class LocalizationXMLFileHandler extends DefaultHandler { private String temp; Localized localized; List<Localized> localizedList; Map<Language, String> tempMap; /* * When the parser encounters plain text (not XML elements), * it calls(this method, which accumulates them in a string buffer */ public void characters(char[] buffer, int start, int length) { temp = new String(buffer, start, length); } /* * Every time the parser encounters the beginning of a new element, * it calls this method, which resets the string buffer */ public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { temp = ""; if (qName.equalsIgnoreCase("tu")) { localized = new Localized(); localized.setUniqueName(attributes.getValue("tuid")); } else if(qName.equalsIgnoreCase("tuv")) { tempMap.put(Language.getLanguageFromCode(attributes.getValue("xml:lang")), ) } } } }我正在嘗試將此 xml 文件中的數據提取到包含語言枚舉和本地化名稱的 tempMap 中。
Java 使用 SAX 解析讀取 XML
慕桂英4014372
2023-08-16 17:39:42