- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我开始使用 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/
我一直在尝试为 SAX 解析器设置 UI 线程和处理程序。这是我没有实现 UI 线程和处理程序的解析器: public class AndroidXMLReader extends ListActiv
我正在尝试读取一个大型 XML 文档,并且我想以 block 的形式读取它,而不是 XmlDocument 将整个文件读入内存的方式。我知道我可以使用 XmlTextReader 来做到这一点,但我想
我正在尝试将 11384 个 XML 文件解析到一个 SQLite 数据库中。其中之一: ]> 1 2 我正在使用 SAX 解析器: pub
我需要用 Perl 解析 XML 文件。文件的一部分单独存储,并作为系统实体插入。不过,这个问题很常见。但是我无法获得任何信息来解决它。 ]> &externalContent; 当使
我正在尝试从 xsd 解析 HL7 消息定义。我将模式定义分成两个文件。第一个文件包含实际的消息定义,第二个文件包含消息中的段定义。 我正在尝试调整示例代码以从此处解析 XML https://gis
我正在使用 SAX 解析 MathML 表达式(尽管它是 MathML 的事实可能并不完全相关)。输入字符串示例为 λ 为了让 SAX 解
我正在尝试使用 Java 和 SAX 为 Android 设备解析 XML 文件。我从互联网上获取,在解析它时,我得到一个 ExpatException :字符“é”的格式不正确(无效标记)。有没有办
我正在尝试使用java中的sax读取xml文件。我只获得 endElement 的输出,但无法找出 startElement 出了什么问题。 这是我的处理程序: public class XMLHan
我正在使用 SAX 解析器来解析具有父级及其子级标记的 XML,如下所示:
我正在尝试从 RSS 提要中提取数据。 RSS 链接 - http://www.thehindu.com/sport/?service=rss ? 这是我的默认处理程序的字符方法。 public vo
我尝试执行下面的代码,但我的 SAX 解析器没有调用 startElement 方法。 下面是我的代码: package getTableStructure; import java.util.Lis
我正在满足一项要求,即我需要拆分大型 XML 并进一步处理。 这是 XML 示例,它可以变成单行。 yongjin 这是我的代码: import java.util.Arrays; import ja
我正在尝试将标签的内容放入我的 java Sax 解析器中的变量中。但是,Characters 方法仅返回 Char 数组。有没有办法将 Char 数组转换为 Int??? public void c
我有下面的代码.. System.setProperty("http.proxyHost","176.6.129.25") ; System.setProp
如何使用 SAX 显示树中最大深度的节点名称。该算法很适合我理解这个概念.. 例如,我应该如何使用 startelement、endelement、startdocument、enddocument
有没有可行的方法使用默认的处理程序类来查找对应的XML标签?例如... 1 1 我想使用 startElement() 和 endElement() 方
我想使用 SAX 解析器从 xml 文件中解析一些数据。我的xml如下: Pies & past Fruits 为了解析这些数据,我扩展了 DefaultHandler。 解析后的输出
我正在构建一个创建 XML 的流程(从各种来源并出于我事先不知道的各种目的),并且我希望将生成的 XML 直接注入(inject)到标准 XML 处理中,例如 SAX、StAX和 DOM。我已经完成了
我有一个由 MS Excel 创建的 XML 文件,其中包含如下元素: 22. Department"GS "NAES "ABCDEF"
我在使用 java sax 解析器打开 stackoverflow 帖子 XML 转储时遇到问题。它识别每个元素的结尾,但似乎跳过了 startElement 方法。我使用示例代码: try {
我是一名优秀的程序员,十分优秀!