更改xml值在java中作为字符串

时间:2015-02-03 10:22:31

标签: java xml string

String
-------------------
<DslOrder index="1422796670990">
<orderId index="1422796671001">123456</orderId>
<AsdlServiceDetails index="1422796670991">
<bandwidth index="1422796670993">45678</bandwidth>
</AsdlServiceDetails>
<SubscriberInfo index="1422796670994">
<Address index="1422796670996">

<City index="1422796670999">MO</City>

<PostalCode index="1422796670998">456</PostalCode>

<Street index="1422796670997">sdfghh</Street>

</Address>

<PhoneNumber index="1422796671000">45678</PhoneNumber>

<Name index="1422796670995">sedrtfgh</Name>

</SubscriberInfo>

</DslOrder>

我有这个字符串。我需要在Xml Elements之间更改String的值。 我如何动态实现这一目标。

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用data projection来解决这个问题(披露:我隶属于该项目)。您可以为xml数据定义“投影”(类似于视图)。此投影是一个接口,用于定义从XML读取,创建更新或删除内容的方法。使用XPath表达式声明要做什么:

public class DSLOrder {

    private final static String XML_BEFORE="<DslOrder index=\"1422796670990\">\n" + 
            "<orderId index=\"1422796671001\">123456</orderId>\n" + 
            "<AsdlServiceDetails index=\"1422796670991\">\n" + 
            "<bandwidth index=\"1422796670993\">45678</bandwidth>\n" + 
            "</AsdlServiceDetails>\n" + 
            "<SubscriberInfo index=\"1422796670994\">\n" + 
            "<Address index=\"1422796670996\">\n" + 
            "<City index=\"1422796670999\">MO</City>\n" + 
            "<PostalCode index=\"1422796670998\">456</PostalCode>\n" + 
            "<Street index=\"1422796670997\">sdfghh</Street>\n" + 
            "</Address>\n" + 
            "<PhoneNumber index=\"1422796671000\">45678</PhoneNumber>\n" + 
            "<Name index=\"1422796670995\">sedrtfgh</Name>\n" + 
            "</SubscriberInfo>\n" + 
            "</DslOrder>";


    public interface Projection {
        // Define any methods for manipulation here       
        @XBUpdate("/DslOrder/SubscriberInfo/PhoneNumber")
        void updatePhoneNumber(String newNumber);

        // Define any methods for manipulation here       
        @XBUpdate("/DslOrder/SubscriberInfo/Name")
        void updateName(String newName);

        @XBWrite("/DslOrder/SubscriberInfo/additionalTag")
        void createAdditionalTag(String value);
    }

    public static void main(String[] args) {
        Projection projection = new XBProjector(Flags.TO_STRING_RENDERS_XML).onXMLString(XML_BEFORE).createProjection(Projection.class);
        projection.updatePhoneNumber("12345");
        projection.updateName("New Name");
        projection.createAdditionalTag("value for new tag");
        System.out.println(projection.toString());
    }
}

此程序打印出来:

<DslOrder index="1422796670990">
<orderId index="1422796671001">123456</orderId>
<AsdlServiceDetails index="1422796670991">
<bandwidth index="1422796670993">45678</bandwidth>
</AsdlServiceDetails>
<SubscriberInfo index="1422796670994">
<Address index="1422796670996">
<City index="1422796670999">MO</City>
<PostalCode index="1422796670998">456</PostalCode>
<Street index="1422796670997">sdfghh</Street>
</Address>
<PhoneNumber index="1422796671000">12345</PhoneNumber>
<Name index="1422796670995">New Name</Name>
<additionalTag>value for new tag</additionalTag>
  </SubscriberInfo>
</DslOrder>

当然,您需要将方法添加到您需要的投影中。这些投影方法甚至可以在XPath表达式中使用参数(动态投影)。