使用 SAX 的状态模式
我必须使用 java SAX 解析器解析一些 xml。 由于忙,看到这里可以使用状态模式。
定义了清晰的状态和状态转换,类似于 xml 文档的结构。
为了实现状态模式,我必须定义一个接口(或抽象类)。 最明显的接口方法是:
public void startElement(String elementName);
public void endElement(String elementName);
但我遇到的问题是如何返回信息。 我需要一个结构中 xml 文档中多个级别的信息。
这是 xml 文档的一部分,
<chessboard>
<white>
<king>
<position>
<x>e</x>
<y>1</y>
</position>
</king>
<pawns>
<pawn id="1">
<position>
<x>e</x>
<y>2</y>
</position>
</pawn>
<pawn id="1">
<position>
<x>f</x>
<y>2</y>
</position>
</pawn>
</pawns>
</white>
</chessboard>
我的假设是否正确,状态模式适合这里? 如果是这样,在这里实施它的最佳方法是什么?
I have to parse some xml with java SAX parser. As I was busy, I saw that the state pattern could be used here.
There are clear states and state transitions defined, resembling the structure of the xml document.
To implement the state pattern, I have to define a interface (or abstract class). The most obvious interface methods would be:
public void startElement(String elementName);
public void endElement(String elementName);
But the problem I encounter is how to return the information. I need the information from several levels in the xml document in one structure.
This is a part of the xml document
<chessboard>
<white>
<king>
<position>
<x>e</x>
<y>1</y>
</position>
</king>
<pawns>
<pawn id="1">
<position>
<x>e</x>
<y>2</y>
</position>
</pawn>
<pawn id="1">
<position>
<x>f</x>
<y>2</y>
</position>
</pawn>
</pawns>
</white>
</chessboard>
Is my assumption right that the state patterns fits here? And if so, what is the best way to implement it here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的接口可以由类来实现,这些类的实例“内部累积”特定的所需信息,并添加其他方法以使该信息可访问。
然而,很难有通用的“获取信息”方法,因为类型可能会有所不同; 而且,这整个方法可能不太适合 SAX 类方法的主要优点,即随着解析的进行而增量执行操作的能力,而不是在解析期间累积信息并仅在稍后执行(此优点在以下情况下可能至关重要)您正在解析非常大的文档)。 因此,我不会将其视为在 Java 中使用 SAX 的“规范”方式,而只是将其视为偶尔会派上用场的一种变体。
Your interface could be implemented by classes whose instances "internally accumulate" the specific required information, and add other methods to make that information accessible.
However, it's difficult to have general-purpose "get the info" methods, since types can vary; and, this whole approach may not fit well with the main advantage of SAX-like approaches, which is the ability to perform actions incrementally as the parse proceeds, rather than accumulating info during the parse and only acting later (this advantage can be crucial when you're parsing extremely large documents). So, I wouldn't take it as a "canonical" way to employ SAX in Java, just as one variation which may come in handy once in a while.
我认为状态模式非常适合这里。 必须保留一些状态,才能知道您在文档中的位置。
要收集信息,您可以将一个对象传递给状态类的每个方法,以便它们可以将信息放入其中。
除非还有其他我没有想到的问题,我认为这是最好的解决方案。
I think state pattern just fits here. There has to be some state kept, to know where you are in the document.
To gather the information, you can pass an object to each method of the stateclasses, so they can put their information in there.
Unless there is some other problem I haven't thought of, I think this is the best sollution.