StAX XML 两个必需标签之间的所有内容

StAX XML all content between two required tags(StAX XML 两个必需标签之间的所有内容)
本文介绍了StAX XML 两个必需标签之间的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

开始学习StAX,使用XMLStreamReader,我遇到了一些问题.如何将标签之间的所有内容作为文本获取?我的意思是,我知道所需标签的名称,当我找到它时,我必须转到关闭标签,并且我在它们之间找到的所有内容都必须附加到某个字符串.例如,我们有类似

Starting learning the StAX, using XMLStreamReader, I faced with some problem. How can I get ALL content between tags as Text? I mean, I know name of needed tag, and when I find it, I must go to the close tag, and everything I found between them I must append at some string. For example, we have something like

<rootTag>
...    
    <someTag>
        Some text content and other tags here…
    </someTag >
    <tagINeed>
        <someinternalTag1>
            <someinternalTag11>
                Some text content..
            </someinternalTag11>
            ...
        </someinternalTag1>
        <someinternalTag2>
            Something here
        </someinternalTag2>
    </tagINeed>
...
    <somethingAnother>
...
    </somethingAnother >
...
</rootTag>    

所以,我需要得到我的字符串

So, I need to get my string as

        <someinternalTag1>
            <someinternalTag11>
                Some text content..
            </someinternalTag11>
            ...
        </someinternalTag1>
        <someinternalTag2>
            Something here
        </someinternalTag2>

我怎样才能得到它?也许,我必须在源xml中找到所需块的开始和结束偏移量,并在解析后给出子字符串?

How can I get it? Maybe, I must find start and end offsets of needed block in source xml, and give substring after parsing?

推荐答案

试试

    StringWriter sw = new StringWriter();
    XMLOutputFactory of = XMLOutputFactory.newInstance(); 
    XMLEventWriter xw = null;
    XMLInputFactory f = XMLInputFactory.newInstance();
    XMLEventReader xr = f.createXMLEventReader(new FileInputStream("test.xml"));
    while (xr.hasNext()) {
        XMLEvent e = xr.nextEvent();
        if (e.isStartElement()
                && ((StartElement) e).getName().getLocalPart().equals("tagINeed")) {
            xw = of.createXMLEventWriter(sw);
        } else if (e.isEndElement()
                && ((EndElement) e).getName().getLocalPart().equals("tagINeed")) {
            break;
        } else if (xw != null) {
            xw.add(e);
        }
    }
    xw.close();
    System.out.println(sw);

打印

    <someinternalTag1>
        <someinternalTag11>
            Some text content..
        </someinternalTag11>
    </someinternalTag1>
    <someinternalTag2>
        Something here
    </someinternalTag2>

更新:

如果你也需要XML字符串,我们可以这样写:

If you need XML string with too, we can write like that:

        if (e.isStartElement() &&
                ((StartElement) e).getName().getLocalPart().equals("tagINeed")){
            xw = of.createXMLEventWriter(sw);
            xw.add(e);
        } else if (e.isEndElement() &&
                ((EndElement) e).getName().getLocalPart().equals("tagINeed")){
            xw.add(e);
            break;
        } else if (xw != null) {
            xw.add(e);
        }

这篇关于StAX XML 两个必需标签之间的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)