Android - 从 xml 解析级联类型
我正在尝试从 xml 文件解析级联类型(不确定这是否是正确的术语)。
我所说的级联类型是指 xml 元素的属性 ntype
可以是以下任何一个。
<ntype A>
<ntype A:A1>
<ntype A:A2>
<ntype B>
<ntype B:B1>
<ntype B:B2>
...
这类似于 android
xml 文件中的 layout
规范,例如 android:layout_width
、android:layout_height
等。 ,
ntype
决定 xml 文件的类型,并在此基础上完成数据的进一步处理。
目前,我解析此问题的方法是将所有 ntype
放在一个枚举数据类型中,如下所示:(通过从 ntype 中删除“:”)
public static enum NType {
A,
AA1,
AA2,
B,
BB1,
BB2,
...
}
在解析器中我这样做
NType nType = NType.valueof(attributes.getValue("ntype").replace(":","");
但是,如果一个新的ntype
已添加到 xml 文件中,但该类型不在 enum
中,应用程序崩溃。我想知道是否有更好的方法来存储这些级联类型的数据,以及如果 ntype
不存在,我应该如何添加默认行为,以便应用程序不会崩溃。
任何帮助表示赞赏。
PS:我对理论计算机科学的了解稍少,我相信这主要是一个关于应该使用编程语言中的哪些/什么/如何工具的理论问题。
I am trying to parse cascaded types(not sure if this is the right term) from xml files.
What I mean by cascaded types is that the attribute ntype
of an xml element could be any of the following ones.
<ntype A>
<ntype A:A1>
<ntype A:A2>
<ntype B>
<ntype B:B1>
<ntype B:B2>
...
This is similar to the layout
specifications in the android
xml files like android:layout_width
,android:layout_height
etc.,
The ntype
decides the type of the xml file and based on that further processing of the data is done.
Currently the way I'm parsing this is by having all the ntype
s in an enumerated datatype like this: (By removing the ":" from the ntype)
public static enum NType {
A,
AA1,
AA2,
B,
BB1,
BB2,
...
}
In the parser I do
NType nType = NType.valueof(attributes.getValue("ntype").replace(":","");
However, if a new ntype
is added to the xml file and that type is not there in the enum
the app crashes. I wanted to know if there is any better way of storing these cascaded types of data and how should I add a default behavior if the ntype
doesn't exist so that the app doesn't crash.
Any help is appreciated.
PS: I am slightly less knowledgeable in theoretical computer science and I believe this is mostly a theoretical question as to which/what/how tools from programming language should be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定它是否有效,因为您的问题有点抽象,我不知道您的 ntypes 是什么,也不知道处理文档时需要做什么。怎么样...
您可以执行两遍解析操作并创建某种类型的集合(可能是
List
,尽管其他集合可能更合适),而不是创建静态枚举)。简而言之,我将运行第一遍并使用集合的
contains(Object E)
方法来查看它是否已包含该 ntype - 如果没有,则使用add(Object E)< /code> 方法来添加它。对整个 xml 文档执行此操作。
第二遍,根据需要处理文档,但(假设您想要一个表示 ntype 的 int 值)使用集合
indexOf(Object E)
方法。通过在第一次传递时解析文档并编译所有
ntype
,您将动态地确保您了解所有这些ntype
,而不会遇到未知ntype
的风险。Not sure if it will work as your question is a little abstract and I don't know what your
ntypes
are or what you need to do when processing the document. How about this...Rather than create your static enum, you could do a two-pass parsing operation and create a collection of some sort (perhaps a
List<String>
although other collections may be more suitable).In short, I'd run a first pass and use the collection's
contains(Object E)
method to see if it already contains that ntype - if not then use theadd(Object E)
method to add it. Do this for the whole xml document.Second pass, process the document as you need to but (assuming you want an int value representing the ntype) use the collections
indexOf(Object E)
method.By parsing the document and compiling all of the
ntypes
on the first pass, you'll be dynamically ensuring that you know all of them without risk of coming across an unknownntype
.