簡體   English   中英

使用SimpleXML反序列化包含其他xml標記的XML元素作為單個字符串

[英]Deserializing an XML element containing other xml markup as a single string using SimpleXML

我已經使用了一段時間的SimpleXML來序列化我的java對象,但是我仍然在學習,有時會遇到麻煩。 我有以下要反序列化的XML:

<messages>
<message>
    <text>
       A communications error has occurred. Please try again, or contact  <a href="someURL">administrator</a>. Alternatively, please <a href = "someURL' />">register</a>. 
    </text>       
</message>

我想對其進行處理,以便將元素的內容視為單個字符串,並忽略錨標簽。 我無法控制此XML的生成方式-如您所見,它是來自某些服務器的錯誤消息。 我該如何實現? 提前謝謝了。

您可能要嘗試通過導入來轉義文本:

import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;

並將其用作:

a.setWordCloudStringToDisplay(escapeHtml(wordcloud));

簡單的XML基本上不提供讀取文本和Element的功能。 您必須使用Converter。 您可以閱讀https://stackoverflow.com/questions/17462970/simpleframwork-xml-element-with-inner-text-and-child-elements ,除了只讀取一個文本外,它回答了完全相同的問題。

這是在單個字符串中獲取多個文本和href的解決方案。

首先,我為'a'標簽創建一個A類,使用toString方法來按xml中的原樣打印標簽:

@Root(name = "a")
public class A {
    @Attribute(required = false)
    private String href;
    @Text
    private String value;

    @Override
    public String toString(){
        return "<a href = \"" + href + "\">" + value + "</a>";
    }
}

然后,使用Text類讀取“文本”,其中需要進行轉換:

@Root(name = "Text")
@Convert(Text.Parsing.class)
public class Text {

    @Element
    public String value;

    private static class Parsing implements Converter<Text> {
        // to read <a href...>
        private final Serializer ser = new Persister();

        @Override
        public Text read(InputNode node) throws Exception {
            Text t = new Text();
            String s;
            InputNode aref;

            // read the begining of text (until first xml tag)
            s = node.getValue();
            if (s != null) { t.value = s; }
            // read first tag (return null if no more tag in the Text)
            aref = node.getNext();
            while (aref != null) {
                // add to the value using toString() of A class
                t.value = t.value + ser.read(A.class, aref);
                // read the next part of text (after the xml tag, until the next tag)
                s = node.getValue();
                // add to the value
                if (s != null) { t.value = t.value + s; }
                // read the next tag and loop
                aref = node.getNext();
            }
            return t;
        }

        @Override
        public void write(OutputNode node, Text value) throws Exception {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
}

請注意,我使用標准的序列化程序讀取了'a'標記,並在A類中添加了toString方法以將其作為xml字符串取回。 我還沒有找到一種將'a'標簽直接讀取為文本的方法。

還有主類(別忘了AnnotationStrategy,它將Convert方法映射到文本元素的反序列化):

public class parseText {
  public static void main(String[] args) throws Exception {
    Serializer serializer = new Persister(new AnnotationStrategy());
    InputStream in = ClassLoader.getSystemResourceAsStream("file.xml");
    Text t = serializer.read(Text.class, in, false);

    System.out.println("Texte : " + t.value);
  }
}

當我將其與以下xml文件一起使用時:

<text>
    A communications error has occurred. Please try again, or contact <a href="someURL">administrator</a>.
    Alternatively, please <a href = "someURL' />">register</a>. 
</text>

它給出以下結果:

Texte : 
   A communications error has occurred. Please try again, or contact <a href = "someURL">administrator</a>.
   Alternatively, please <a href = "someURL' />">register</a>. 

希望這可以幫助您解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM