第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將原始數(shù)據(jù)轉(zhuǎn)換為自定義的 xml

將原始數(shù)據(jù)轉(zhuǎn)換為自定義的 xml

函數(shù)式編程 2023-09-20 15:15:51
我想使用 Java 將原始文件轉(zhuǎn)換為以下格式 -原始輸入:state | abc country | FR-FRA輸出:<data attr ="StateFr">abc</data> <data attr ="country">FR-FRA</data>州屬性應(yīng)附加上面顯示的國家/地區(qū)代碼。有人可以幫我解決這個問題嗎?
查看完整描述

3 回答

?
至尊寶的傳說

TA貢獻1789條經(jīng)驗 獲得超10個贊

Java Stream API 可以提供幫助


    String raw ="name1|value1\n" +

                    "name2|value2";

    String template = "<data attribute=\"%s\">%s</data>";

    String output = Arrays.stream(raw.split("\n"))

            .map(rawPair -> rawPair.split("\\|"))

            .map(pair -> String.format(template, pair[0], pair[1]))

            .collect(Collectors.joining("\n"));

將輸出


<data attribute="name1">value1</data>

<data attribute="name2">value2</data>

但擁有特定的業(yè)務(wù)邏輯需要更多的動作。首先獲取國家代碼,然后在流處理上裝飾您的屬性名稱


    BiFunction<String, String, String> decorate = (String name, String code) -> {

        if ("state".equals(name)) {

            return name + code;

        } else {

            return name;

        }

    };

    Function<String, String> countryCode = (String source) -> {

        String head = "country|";

        int start = source.indexOf(head) + head.length();

        return source.substring(start, start + 2);

    };


    String code = countryCode.apply(raw);


    ...

    .map(pair -> String.format(template, decorate.apply(pair[0], code), pair[1]))

    ...


查看完整回答
反對 回復(fù) 2023-09-20
?
白板的微信

TA貢獻1883條經(jīng)驗 獲得超3個贊

有了新的要求

  1. 原始文件很大

  2. 原始文件的國家/地區(qū)代碼位于州旁邊

  3. 一份一份地讀取文件。

  4. 還需要按照原始源中出現(xiàn)的順序輸出轉(zhuǎn)換后的條目。

你應(yīng)該

  1. 識別狀態(tài)并保留它,尚未生成下一個條目

  2. 識別后續(xù)國家,更新保存的狀態(tài)并釋放州和國家條目

所以在這里我為這個角色使用了某種淺緩沖區(qū)

   String raw = "name|value1\n" +

            "state|some-state1\n" +

            "country|fr-fra\n" +

            "name|value2\n" +

            "state|some-state2\n" +

            "country|en-us\n";


    class ShallowBuffer {

        private String stateKey = "state";

        private String countryKey = "country";

        private String[] statePairWaitingForCountryCode = null;


        private List<String[]> pump(String[] pair) {

            if (stateKey.equals(pair[0])) {

                statePairWaitingForCountryCode = pair;

                return Collections.emptyList();

            }

            if (countryKey.equals(pair[0])) {

                statePairWaitingForCountryCode[0] = statePairWaitingForCountryCode[0] + pair[1].substring(0, 2);

                String[] stateRelease = statePairWaitingForCountryCode;

                statePairWaitingForCountryCode = null;

                return Arrays.asList(stateRelease, pair);

            }

            return Collections.singletonList(pair);

        }

    }


    ShallowBuffer patience = new ShallowBuffer();

    String template = "<data attribute=\"%s\">%s</data>";

    String output = Arrays.stream(raw.split("\n"))

            .map(rawPair -> rawPair.split("\\|"))

            .map(patience::pump)

            .flatMap(Collection::stream)

            .map(pair -> String.format(template, pair[0], pair[1]))

            .collect(Collectors.joining("\n"));

這將輸出


 <data attribute="name">value1</data>

 <data attribute="statefr">some-state1</data>

 <data attribute="country">fr-fra</data>

 <data attribute="name">value2</data>

 <data attribute="stateen">some-state2</data>

 <data attribute="country">en-us</data>

淺緩沖區(qū)是可變的,因此您不能在流鏈中使用并行方法。這也意味著將其標(biāo)記為可訪問范圍之外將需要同步工作。并且您仍然需要將國家/地區(qū)代碼的第一個字母大寫)


查看完整回答
反對 回復(fù) 2023-09-20
?
aluckdog

TA貢獻1847條經(jīng)驗 獲得超7個贊

運行以下 XSLT 3.0 樣式表:


<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"?

? ? ?version="3.0" expand-text="yes" xmlns:f="f">


<xsl:template name="xsl:initial-template">

?<root>

? <xsl:iterate select="unparsed-text-lines('input.txt')">

? ? <xsl:param name="prev-parts" select="()"/>

? ? <xsl:on-completion>

? ? ? ?<attribute name="{$prev-parts[1]}">{$prev-parts[2]}</attribute>

? ? </xsl:on-completion>??

? ? <xsl:variable name="parts" select="tokenize(., '\|')"/>

? ? <xsl:choose>

? ? ? <xsl:when test="$parts[1] = 'country'">

? ? ? ? <attribute name="{f:titleCase($prev-parts[1])}{f:titleCase(substring-before($parts[2], '-')}">{$prev-parts[2]}</attribute>

? ? ? </xsl:when>

? ? ? <xsl:otherwise>

? ? ? ? <attribute name="{$prev-parts[1]}>{$prev-parts[2]}</attribute>

? ? ? </xsl:otherwise>

? ? </xsl:choose>

? ? <xsl:next-iteration>

? ? ? <xsl:with-param name="prev-parts" select="$parts"/>

? ? </xsl:next-iteration>

? </xsl:iterate>

?</root>

</xsl:template>


<xsl:function name="f:titleCase">

? <xsl:param name="in"/>

? <xsl:sequence select="upper-case(substring($in, 1, 1))||substring($in, 2)"/>

</xsl:function>? ??

</xsl:transform>

請注意,與此處介紹的其他解決方案不同,此解決方案始終會生成格式良好的 XML 輸出。


查看完整回答
反對 回復(fù) 2023-09-20
  • 3 回答
  • 0 關(guān)注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號