将XML从标记重新格式化为属性和名称

时间:2015-11-26 13:19:47

标签: java xml

是否有人知道可以重新格式化XML的工具或jar,例如:

<job>
    <id>7562882</id>
    <title>Techniker im Unterhalt (m/w) in Murten</title>
    <deleted>false</deleted>
</job>

要:

<job>
<property name="id">7562882</property>
  <property name="title">Techniker im Unterhalt (m/w) in Murten</property>
 <property name="deleted">false</property>
</job>

child nodes来自:

<company>
        <id>40057210</id>
        <name>Roland Murten AG</name>
</company>

 <property name="company-id">40057210</property>
 <property name="company-name">Roland Murten AG</property>

java中的任何解决方案都会很棒,但因为它不是一件简单的事情,我会在任何OO中使用解决方案。 谢谢。 我不想将值添加为标记的一部分,只是为了重新格式化。

1 个答案:

答案 0 :(得分:1)

以下XSLT转换将执行此操作:

private static void transform(String xml) throws TransformerException {
    String xslt = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" +
                  "  <xsl:output omit-xml-declaration=\"yes\"/>\n" +
                  // Transform child elements of <job>
                  "  <xsl:template match=\"job/*\">\n" +
                  "    <property name=\"{local-name(.)}\"><xsl:value-of select=\".\"/></property>\n" +
                  "  </xsl:template>\n" +
                  // Remove <company>, but transform children
                  "  <xsl:template match=\"company\">\n" +
                  "    <xsl:apply-templates select=\"node()\"/>\n" +
                  "  </xsl:template>\n" +
                  // Transform child elements of <company>
                  "  <xsl:template match=\"company/*\">\n" +
                  "    <property name=\"{local-name(..)}-{local-name(.)}\"><xsl:value-of select=\".\"/></property>\n" +
                  "  </xsl:template>\n" +
                  // Copy all other content unchanged
                  "  <xsl:template match=\"@*|node()\">\n" +
                  "    <xsl:copy>\n" +
                  "      <xsl:apply-templates select=\"@*|node()\"/>\n" +
                  "    </xsl:copy>\n" +
                  "  </xsl:template>\n" +
                  "</xsl:stylesheet>\n";
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer(new StreamSource(new StringReader(xslt)));
    transformer.transform(new StreamSource(new StringReader(xml)),
                          new StreamResult(System.out));
}

测试

public static void main(String[] args) throws Exception {
    transform("<job>\n" +
              "    <id>7562882</id>\n" +
              "    <title>Techniker im Unterhalt (m/w) in Murten</title>\n" +
              "    <deleted>false</deleted>\n" +
              "</job>\n");
    System.out.println();   System.out.println();
    transform("<company>\n" +
              "        <id>40057210</id>\n" +
              "        <name>Roland Murten AG</name>\n" +
              "</company>\n");
}

输出

<job>
    <property name="id">7562882</property>
    <property name="title">Techniker im Unterhalt (m/w) in Murten</property>
    <property name="deleted">false</property>
</job>

        <property name="company-id">40057210</property>
        <property name="company-name">Roland Murten AG</property>