简体   繁体   English

使用SAX xerces解析xml时加载Java对象

[英]Loading java object when parsing xml using SAX xerces

If my xml looks like: 如果我的xml看起来像:

<root version="1.1">
  <node number="1">
    <url>http://....</url>
  </node>
  <url>http://...</url>
</root>

Now in my endElement event I look if the xml element starts with 'url', but how do I know when the url element is the url element inside the node element and when it is not? 现在在endElement事件中,我查看xml元素是否以'url'开头, 但是如何知道url元素是node元素内的url元素,何时不是呢?

public void endElement(String uri, String localName, String qName, Attributes attributes) {
  if(qName.equals("url")) {
    someObject.setUrl(characters);
  }
}

one way is to keep track of elements "seen" either by maintaining a stack of element names or using a flag that indicates you're inside of a subtree. 一种方法是通过维护元素名称堆栈或使用指示您在子树内部的标志来跟踪“可见”的元素。

First way: in startElement push the element name on a stack, then in endElement pop the name off the stack. 第一种方法:在startElement中将元素名称压入堆栈,然后在endElement中将名称弹出堆栈。 When you see "url" in endElement, check if "node" is on the stack. 当您在endElement中看到“ url”时,请检查“ node”是否在堆栈中。

Another way: in startElement set nodeElementSeen = true, then in endElement set nodeElementSeen = false. 另一种方式:在startElement中将nodeElementSeen = true设置为,然后在endElement中将nodeElementSeen = false设置为。 When you see "url" in endElement, check if nodeElementSeen = true. 当您在endElement中看到“ url”时,请检查nodeElementSeen = true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM