将原始数据转换为自定义的 xml

我想使用 Java 将原始文件转换为以下格式 -

原始输入:

state | abc
country | FR-FRA

输出:

<data attr ="StateFr">abc</data>
<data attr ="country">FR-FRA</data>

州属性应附加上面显示的国家/地区代码。有人可以帮我解决这个问题吗?


函数式编程
浏览 93回答 3
3回答

至尊宝的传说

Java Stream API 可以提供帮助&nbsp; &nbsp; String raw ="name1|value1\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name2|value2";&nbsp; &nbsp; String template = "<data attribute=\"%s\">%s</data>";&nbsp; &nbsp; String output = Arrays.stream(raw.split("\n"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(rawPair -> rawPair.split("\\|"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(pair -> String.format(template, pair[0], pair[1]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.joining("\n"));将输出<data attribute="name1">value1</data><data attribute="name2">value2</data>但拥有特定的业务逻辑需要更多的动作。首先获取国家代码,然后在流处理上装饰您的属性名称&nbsp; &nbsp; BiFunction<String, String, String> decorate = (String name, String code) -> {&nbsp; &nbsp; &nbsp; &nbsp; if ("state".equals(name)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return name + code;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; Function<String, String> countryCode = (String source) -> {&nbsp; &nbsp; &nbsp; &nbsp; String head = "country|";&nbsp; &nbsp; &nbsp; &nbsp; int start = source.indexOf(head) + head.length();&nbsp; &nbsp; &nbsp; &nbsp; return source.substring(start, start + 2);&nbsp; &nbsp; };&nbsp; &nbsp; String code = countryCode.apply(raw);&nbsp; &nbsp; ...&nbsp; &nbsp; .map(pair -> String.format(template, decorate.apply(pair[0], code), pair[1]))&nbsp; &nbsp; ...

白板的微信

有了新的要求原始文件很大原始文件的国家/地区代码位于州旁边一份一份地读取文件。还需要按照原始源中出现的顺序输出转换后的条目。你应该识别状态并保留它,尚未生成下一个条目识别后续国家,更新保存的状态并释放州和国家条目所以在这里我为这个角色使用了某种浅缓冲区&nbsp; &nbsp;String raw = "name|value1\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "state|some-state1\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "country|fr-fra\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name|value2\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "state|some-state2\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "country|en-us\n";&nbsp; &nbsp; class ShallowBuffer {&nbsp; &nbsp; &nbsp; &nbsp; private String stateKey = "state";&nbsp; &nbsp; &nbsp; &nbsp; private String countryKey = "country";&nbsp; &nbsp; &nbsp; &nbsp; private String[] statePairWaitingForCountryCode = null;&nbsp; &nbsp; &nbsp; &nbsp; private List<String[]> pump(String[] pair) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (stateKey.equals(pair[0])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; statePairWaitingForCountryCode = pair;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Collections.emptyList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (countryKey.equals(pair[0])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; statePairWaitingForCountryCode[0] = statePairWaitingForCountryCode[0] + pair[1].substring(0, 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] stateRelease = statePairWaitingForCountryCode;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; statePairWaitingForCountryCode = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Arrays.asList(stateRelease, pair);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Collections.singletonList(pair);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ShallowBuffer patience = new ShallowBuffer();&nbsp; &nbsp; String template = "<data attribute=\"%s\">%s</data>";&nbsp; &nbsp; String output = Arrays.stream(raw.split("\n"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(rawPair -> rawPair.split("\\|"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(patience::pump)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap(Collection::stream)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(pair -> String.format(template, pair[0], pair[1]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.joining("\n"));这将输出&nbsp;<data attribute="name">value1</data>&nbsp;<data attribute="statefr">some-state1</data>&nbsp;<data attribute="country">fr-fra</data>&nbsp;<data attribute="name">value2</data>&nbsp;<data attribute="stateen">some-state2</data>&nbsp;<data attribute="country">en-us</data>浅缓冲区是可变的,因此您不能在流链中使用并行方法。这也意味着将其标记为可访问范围之外将需要同步工作。并且您仍然需要将国家/地区代码的第一个字母大写)

aluckdog

运行以下 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 输出。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java