gpt4 book ai didi

Java 使用 SAX 解析读取 XML

转载 作者:行者123 更新时间:2023-12-02 00:26:11 24 4
gpt4 key购买 nike

所以我开始使用 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")), )
}
}

/*
* 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 ,可以接收Parents 属性并获取相同的 seg 值线?

最佳答案

每次点击新的文本节点时,您都​​会覆盖文本缓冲区,包括纯空白文本节点,例如 </seg> 之间的文本节点。和</tuv> 。处理 seg 时需要保存文本缓冲区的内容结束标签,在处理tuv时拾取它结束标记。

此外,您还应该意识到,单个文本节点的内容可以在对 text() 的调用序列中提供:解析器可以以任何它喜欢的方式分解它(许多解析器在实体边界上执行此操作)。您需要通过附加到缓冲区来累积内容。

另请注意,XML 区分大小写;在测试元素名称时,您不应该真正忽略大小写。

当询问有关 SO 的问题时,正确使用术语会有所帮助:将元素称为属性会让人们感到困惑。

关于Java 使用 SAX 解析读取 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58049589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com