替换XML中的内容

时间:2012-04-21 07:17:09

标签: xml xml-parsing batch-processing xslt

我需要编辑一些XML文件,并希望创建一个脚本来执行此操作。我对XML的经验几乎不存在,这就是我要求你帮助的原因。

下面的XML包含2个传输顺序:一个具有正元素,一个具有负值。我想做的是,对于包含负的每个运输订单:

  • pickup_task 的地址代码替换为投放任务的地址代码,反之亦然
  • 使用传递任务的值替换拾取任务的任务窗口(from_instant和till_instant)。

是否可以制作一个VB脚本或批处理或可以替换这些文件的文件,这些文件包含200多个transport_orders?

由于

<?xml version="1.0" encoding="iso-8859-2"?>
<misu version="2010">
    <transport_orders>
        <transport_order>
            <id>NOCCO/12-006798_1</id>
            <order_number>NOCCO/12-006798_1</order_number>
            <order_date>2012-03-30</order_date>
            <contactId>C04396</contactId>
            <productId>0103-01101025</productId>
            <sum>3135.51</sum>
            <currency_code></currency_code>
            <reference>NOCCO/12-006798</reference>
            <amounts>
                <amount>
                    <unit_code>kg</unit_code>
                    <value>324.00</value>
                </amount>
            </amounts>
            <pickup_task>
                <addressid>BUCU</addressid>
                <task_window>
                    <from_instant>2012-04-20T18:26:43</from_instant>
                    <till_instant>2012-04-20T18:26:43</till_instant>
                </task_window>
            </pickup_task>
            <delivery_task>
                <addressid>C04396_1</addressid>
                <task_window>
                    <from_instant>2012-04-23T00:00:00</from_instant>
                    <till_instant>2012-04-24T00:00:00</till_instant>
                </task_window>
            </delivery_task>
        </transport_order>
        <transport_order>
            <id>NOCCO/12-007016_2</id>
            <order_number>NOCCO/12-007016_2</order_number>
            <order_date>2012-04-02</order_date>
            <contactId>C01137</contactId>
            <productId>0604-08L15000</productId>
            <sum>495.00</sum>
            <currency_code></currency_code>
            <reference>NOCCO/12-007016</reference>
            <amounts>
                <amount>
                    <unit_code>m2</unit_code>
                    <value>-5.00</value>
                </amount>
            </amounts>
            <pickup_task>
                <addressid>SFGH</addressid>
                <task_window>
                    <from_instant>2012-04-20T18:26:43</from_instant>
                    <till_instant>2012-04-20T18:26:43</till_instant>
                </task_window>
            </pickup_task>
            <delivery_task>
                <addressid>C01137_1</addressid>
                <task_window>
                    <from_instant>2012-04-23T00:00:00</from_instant>
                    <till_instant>2012-04-24T00:00:00</till_instant>
                </task_window>
            </delivery_task>
        </transport_order>
    </transport_orders>
</misu>

2 个答案:

答案 0 :(得分:0)

在这里,你应该完成第1部分。我将第2部分留给你(我不习惯VB)。如您所见,我将XML文件命名为“data.xml”。

Sub Main()
    Dim xdoc As XDocument
    Dim filename As String
    filename = "../../data/data.xml"
    xdoc = XDocument.Load(filename)
    Dim xe As XElement
    xe = xdoc.Root.Element("transport_orders")
    For Each element In xe.Elements("transport_order")
        Dim val As Integer
        val = element.Element("amounts").Element("amount").Element("value").Value
        If (val < 0) Then
            Console.WriteLine(val)
            Dim puelement As XElement
            Dim delelement As XElement
            Dim putask As String
            puelement = element.Element("pickup_task").Element("addressid")
            delelement = element.Element("delivery_task").Element("addressid")
            putask = delelement.Value
            delelement.Value = puelement.Value
            puelement.Value = putask
        End If
    Next
    xdoc.Save(filename)
    Console.ReadLine()
End Sub

答案 1 :(得分:0)

回到Feed,事情正在发挥作用,因为我已经说过我的知识有限,我总是试图理解代码的作用并尝试重现以满足我的需求所以这就是我为第二个问题所做的,我知道是相当蹩脚但它应该做的,请确认它

puelement2  = element.Element("pickup_task").Element("task_window").Element("from_instant")
delelement2 = element.Element("delivery_task").Element("task_window").Element("from_instant")
putask2 = delelement2.Value
delelement2.Value = puelement2.Value
puelement2.Value = putask2

puelement3 = element.Element("pickup_task").Element("task_window").Element("till_instant")
delelement3 = element.Element("delivery_task").Element("task_window").Element("till_instant")
putask3 = delelement3.Value
delelement3.Value = puelement3.Value
puelement3.Value = putask3  

感谢。真的很有帮助。