[英]Java read XML with SAX Parsing
所以我開始使用 xml 和 SAX 解析器,現在我試圖弄清楚它是如何工作的,我熟悉 JSON 但這似乎不像 Z0ECD11C1D7A287401D148A23F 那樣工作所以這是我正在使用的代碼
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")), )
}
}
/*
* When the parser encounters the end of an element, it calls this method
*/
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("tu")) {
// add it to the list
accList.add(acct);
} else if (qName.equalsIgnoreCase("Name")) {
acct.setName(temp);
} else if (qName.equalsIgnoreCase("Id")) {
acct.setId(Integer.parseInt(temp));
} else if (qName.equalsIgnoreCase("Amt")) {
acct.setAmt(Integer.parseInt(temp));
}
}
}
我試圖從這個 xml 文件中提取數據到包含語言枚舉和本地化名稱的 tempMap 中。
<?xml version="1.0"?>
<tmx version="1.4">
<body>
<tu tuid="@ACCESS_RIGHTS_ACCESS_MODE">
<tuv xml:lang="EN-US">
<seg>Access Mode</seg>
</tuv>
<tuv xml:lang="DE-DE">
<seg>Zugriffsmodus</seg>
</tuv>
<tuv xml:lang="FR-FR">
<seg>Mode d'accès</seg>
</tuv>
<tuv xml:lang="RU-RU">
<seg>Доступ</seg>
</tuv>
<tuv xml:lang="PL-PL">
<seg>Tryb dostępu</seg>
</tuv>
<tuv xml:lang="ES-ES">
<seg>Modo de acceso</seg>
</tuv>
<tuv xml:lang="PT-BR">
<seg>Modo de acesso</seg>
</tuv>
<tuv xml:lang="ZH-CN">
<seg>權限模式</seg>
</tuv>
<tuv xml:lang="KO-KR">
<seg>접근 모드</seg>
</tuv>
</tu>
</body>
</tmx>
現在在 java 代碼的第 49 行,我從 tuv 屬性中獲取語言代碼,但我缺少位於 tuv 下方的本地化名稱,稱為 seg of 可以接收父母屬性並在同一行中獲取 seg 值?
每次點擊新文本節點時,您都會覆蓋文本緩沖區,包括像</seg>
和</tuv>
之間的純空格文本節點。 處理seg
結束標記時需要保存文本緩沖區的內容,處理tuv
結束標記時將其取出。
您還應該知道,單個文本節點的內容可以通過對 text() 的一系列調用提供:解析器可以按照它喜歡的任何方式分解它(許多解析器在實體邊界上這樣做)。 您需要通過附加到緩沖區來積累內容。
另請注意 XML 區分大小寫; 在測試元素名稱時,您不應該真正忽略大小寫。
在提出關於 SO 的問題時,正確使用術語會有所幫助:將元素稱為屬性會使人們感到困惑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.