久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

獲取 xml 作為字符串而不是使用 xstream 的類

get xml as string instead of class with xstream(獲取 xml 作為字符串而不是使用 xstream 的類)
本文介紹了獲取 xml 作為字符串而不是使用 xstream 的類的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有類似 xml 的東西

I have xml something like

<parent>
  <message>
   <type>15</type>
  </message>
</parent>

我需要將 message 表示為 String ,而不是在父對(duì)象中創(chuàng)建消息對(duì)象.所以,當(dāng)我做 parent.message 時(shí),輸出是 <type>15 </type> 而不是消息對(duì)象.

Instead of creating a message object inside parent object, I need to represent message just as a String . So , when I do parent.message , the output is <type> 15 </type> instead of a message object.

推薦答案

idia是通過處理HierarchicalStreamReader來構(gòu)建消息的xml.如果您通過調(diào)用 reader.goDown() 進(jìn)入 <messsage> 不幸的是 reader.getValue() 不會(huì)返回的全部內(nèi)容這個(gè)元素.

The idia is to build up the xml of message by processing the HierarchicalStreamReader. If you go down into <messsage> by calling reader.goDown() unfortunately reader.getValue() does not return the whole content of this element.

型號(hào)

    @XStreamAlias("parent")
    @XStreamConverter(value = ParentConverter.class)
    public class Parent {
        private final String message;

        public Parent(final String message) { this.message = message; }
        public String getMessage() { return message; }
    }

轉(zhuǎn)換器

    public class ParentConverter implements Converter {
        @Override
        public boolean canConvert(@SuppressWarnings("rawtypes") final Class type) {
            return Parent.class.isAssignableFrom(type);
        }

        @Override
        public void marshal(Object source, HierarchicalStreamWriter writer,
                MarshallingContext context) {
            throw new UnsupportedOperationException("unmarshaling only");
        }

        @Override
        public Object unmarshal(HierarchicalStreamReader reader,
                UnmarshallingContext context) {

            reader.moveDown();
            if (!"message".equals(reader.getNodeName())) {
                throw new ConversionException("Expected message, but was "
                        + reader.getNodeName());
            }
            final StringBuilder message = new StringBuilder();
            while (reader.hasMoreChildren()) {
                reader.moveDown();
                buildRecursiveMessage(reader, message);
                reader.moveUp();
            }
            reader.moveUp();

            final Parent parent = new Parent(message.toString());
            return parent;
        }

        private void buildRecursiveMessage(final HierarchicalStreamReader reader,
                final StringBuilder sb) {
            // Build start-tag
            final String nodeName = reader.getNodeName();
            sb.append("<" + nodeName);

            // Build attributes
            final int numAttributes = reader.getAttributeCount();
            if (numAttributes > 0) {
                sb.append(" ");
                for (int i = 0; i < numAttributes; i++) {
                    final String attributeName = reader.getAttributeName(i);
                    final String attributeValue = reader.getAttribute(i);
                    sb.append(attributeName + "="" + attributeValue + """);

                    final boolean lastAttribute = (i == numAttributes - 1);
                    if (!lastAttribute) {
                        sb.append(", ");
                    }
                }
            }

            // Build children
            final boolean containsChildren = reader.hasMoreChildren();
            final boolean containsValue = !reader.getValue().isEmpty();
            final boolean empty = !containsChildren && !containsValue;

            sb.append(!empty ? ">" : " />");

            if (containsChildren) {
                while (reader.hasMoreChildren()) {
                    reader.moveDown();
                    buildRecursiveMessage(reader, sb);
                    reader.moveUp();
                }
            } else if (containsValue) {
                sb.append(reader.getValue());
            }

            // Build end-tag
            if (!empty) {
                sb.append("</" + nodeName + ">");
            }
        }
    }

這個(gè)測(cè)試

    public static void main(String[] args) {
        final XStream xstream = new XStream();
        xstream.processAnnotations(Parent.class);

        // Deserialize
        final String xml = "<parent><message><type>15</type></message></parent>";
        final Parent parent = (Parent) xstream.fromXML(xml);
        System.out.println(parent.getMessage());
    }

打印出來

    <type>15</type>

但內(nèi)容并不完全相同!它忽略例如空格, <foo></foo> 將映射到 <foo/> 并且我沒有測(cè)試像 這樣的 XML 實(shí)體&apos;

But it is not every the same content! It ignores for example whitespaces, <foo></foo> would mapped to <foo /> and I don't tested XML entities like &apos; etc.

也許最好將您的消息包含在 CDATA 標(biāo)記中?喜歡

Maybe it's better to enclose your message in CDATA tags? Like

    <parent>
      <message>
       <![CDATA[
       <type>15</type>
       ]]>
      </message>
    </parent>

這篇關(guān)于獲取 xml 作為字符串而不是使用 xstream 的類的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Upload progress listener not fired (Google drive API)(上傳進(jìn)度偵聽器未觸發(fā)(Google 驅(qū)動(dòng)器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅(qū)動(dòng)api服務(wù)賬戶查看上傳文件到谷歌驅(qū)動(dòng)使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務(wù)帳號(hào)返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 欧美日韩高清在线观看 | 国产999精品久久久久久绿帽 | 一级在线毛片 | 国产ts一区 | 国产一区二区三区在线免费观看 | 亚洲aⅴ | 91精品国产一区二区三区 | 欧美一级电影免费观看 | 国产乱码精品1区2区3区 | 久久久久成人精品亚洲国产 | 午夜免费小视频 | 中文字幕久久精品 | 久久久91精品国产一区二区三区 | 二区在线观看 | 成人av免费 | 中文字幕1区2区 | 美女张开腿露出尿口 | 国际精品鲁一鲁一区二区小说 | 亚洲成人中文字幕 | 91久久久久久 | 玖玖玖在线 | 欧美成人一区二区 | 黄在线免费观看 | 三级在线免费 | 久久久国产精品一区 | 日韩在线视频免费观看 | 中文字幕精品一区久久久久 | 欧美色综合 | 日日精品 | 97人人澡人人爽91综合色 | www成人免费 | 欧美成人一级 | 中文字幕不卡在线88 | 成人不卡一区二区 | 亚洲视频一区在线观看 | 9久久精品| 中文一区二区视频 | 成人免费小视频 | 99久久精品免费看国产高清 | 亚洲欧美另类在线 | 成人免费黄视频 |