[英]Get element “text” by looking for it ID in XML (Stringtable for games) using StaX
到目前為止,我有辦法通過它的 ID 找到我想要的元素 SELECT 但我試過但無法獲得它的內容(消息)。
public static String GetText(String id) throws FileNotFoundException, XMLStreamException
{
File file = new File("C:/Users/LENOVO/IdeaProjects/CasinoRoyale/src/Stringtable.xml");
// Instance of the class which helps on reading tags
XMLInputFactory factory = XMLInputFactory.newInstance();
// Initializing the handler to access the tags in the XML file
XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(file));
String text = "";
// Checking the availability of the next tag
while (eventReader.hasNext()) {
XMLEvent xmlEvent = eventReader.nextEvent();
Characters characters = xmlEvent.asCharacters();
if (xmlEvent.isStartElement()) {
StartElement startElement = xmlEvent.asStartElement();
Iterator<Attribute> iterator = startElement.getAttributes();
while (iterator.hasNext()) {
Attribute attribute = iterator.next();
QName name = attribute.getName();
if ("id".equalsIgnoreCase(name.getLocalPart())) {
String currentId = String.valueOf(attribute.getValue());
if (currentId.equals(id))
text = characters.getData();
break;
}
}
}
}
return text;
}
這就是方法,到目前為止它在 IDE 中沒有給出錯誤。
<?xml version="1.0" encoding="UTF-8"?>
<Stringtable>
<GameHelper id="welcomeTo">Welcome to Casino Royale!</GameHelper>
</Stringtable>
這是我目前的短 Stringtable.xml。
System.out.println(UtilitiesGame.GetText("welcomeTo"));
我就是這樣稱呼它的,它會拋出這個異常:
Exception in thread "main" java.lang.ClassCastException: class com.sun.xml.internal.stream.events.StartDocumentEvent cannot be cast to class javax.xml.stream.events.Characters (com.sun.xml.internal.stream .events.StartDocumentEvent and javax.xml.stream.events.Characters are in module java.xml of loader 'bootstrap') at java.xml/com.sun.xml.internal.stream.events.DummyEvent.asCharacters(DummyEvent.java :115) 在 UtilitiesGame.GetText(UtilitiesGame.Z93F725A07423FE1C8 89F448B33D21F46Z:66) 在 GameHelper.(GameHelper.java:67) 在 CasinoRoyale.main(CasinoRoyale.java:7)
我想要的是給定 ID (welcomeTo) 我得到“歡迎來到皇家賭場!”
拋出異常是因為事件讀取器仍然指向 startElement。
如果您移動角色元素的實例化,您可以輕松修復它
Characters characters = xmlEvent.asCharacters();
這里修復
public static String GetText(String id) throws FileNotFoundException, XMLStreamException
{
File file = new File("C:/Users/LENOVO/IdeaProjects/CasinoRoyale/src/Stringtable.xml");
// Instance of the class which helps on reading tags
XMLInputFactory factory = XMLInputFactory.newInstance();
// Initializing the handler to access the tags in the XML file
XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(file));
String text = "";
// Checking the availability of the next tag
while (eventReader.hasNext()) {
XMLEvent xmlEvent = eventReader.nextEvent();
if (xmlEvent.isStartElement())
{
// Characters characters = xmlEvent.asCharacters();
StartElement startElement = xmlEvent.asStartElement();
Iterator<Attribute> iterator = startElement.getAttributes();
while (iterator.hasNext())
{
Attribute attribute = iterator.next();
QName name = attribute.getName();
if ("id".equalsIgnoreCase(name.getLocalPart()))
{
String currentId = String.valueOf(attribute.getValue());
if (currentId.equals(id))
{
Characters characters = (Characters) eventReader.nextEvent();
text = characters.getData();
}
break;
}
}
}
}
return text;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.