【问题标题】:Capturing mixed content in XML using a SAX Parser使用 SAX 解析器捕获 XML 中的混合内容
【发布时间】:2010-02-10 22:04:34
【问题描述】:

SAX 解析器是否能够捕获 XML 文档中的混合内容(参见下面的示例)?

<element>here is some <b>mixed content</b></element>

【问题讨论】:

    标签: java xml sax


    【解决方案1】:

    当然。您会收到以下事件:

    1. startElement(元素)
    2. 字符(“这里有一些”)
    3. startElement (b)
    4. 字符(“混合内容”)
    5. endElement (b)
    6. endElement(元素)

    【讨论】:

      【解决方案2】:

      是的,虽然我不确定您所说的捕获是什么意思。如果您运行下面的简短示例,您会看到 startElement 处理程序同时为 elementb 调用:

      String xml = "<element>here is some <b>mixed content</b></element>";
      SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
      parser.parse(new ByteArrayInputStream(xml.getBytes()), new DefaultHandler(){
          @Override
          public void startElement(String uri, String localName, String name,
                  Attributes attributes) throws SAXException {
              System.out.println("Started: "+name);
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2016-10-15
        • 2018-06-12
        • 1970-01-01
        • 1970-01-01
        • 2021-09-19
        • 2012-08-27
        • 2011-06-29
        • 2013-01-18
        • 1970-01-01
        相关资源
        最近更新 更多