有没有一个工具可以自动用动态生成的 GUID 替换一堆 GUID?

发布于 2024-08-13 17:47:19 字数 1599 浏览 1 评论 0原文

我不是开发人员。

我需要破解一个 XML 文件来复制数百个资源,每个资源都分配有一个 GUID。有没有一种方法可以解析整个文件,用动态生成的 GUID 替换标签中找到的每个 GUID?

基本上 - 每个 UniqueID 标签(但不是 ContentUniqueID 标签)都需要一个新的 GUID。

<root xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
  <Name>[redacted]</Name>
  <UniqueId>7a136c33-3ea8-4f99-8f99-bbe411972203</UniqueId>
  <Enabled>true</Enabled>
  <Behavior>EmptyOnly</Behavior>
  <Subscriptions />
  <ScheduledFormatTemplates>
    <ScheduledFormatTemplate>
      <Name>[redacted]</Name>
      <UniqueId>1cfaba3e-bfd5-4d2f-a1df-14020ad2f7da</UniqueId>
      <ContentUniqueId>67c58741-fe1b-4c15-8dc0-8b4c01f6f18f</ContentUniqueId>
      <ScheduledContents>
        <ScheduledContent xsi:type="SFTR">
          <Name>[redacted]</Name>
          <UniqueId>b4a60646-b62b-43e7-b2a2-7d37875ab33f</UniqueId>
          <ContentUniqueId>ba634a97-9faf-4bfa-a9b4-d8a2475b82e6</ContentUniqueId>
          <ScheduledContents>
            <ScheduledContent>
              <Name>[redacted]</Name>
              <UniqueId>6f8e6e6c-1f94-4caa-8730-6859448138eb</UniqueId>
              <ContentUniqueId>938b0a24-4043-4a16-bc2d-25dbdb21a659</ContentUniqueId>
            </ScheduledContent>
          </ScheduledContents>
        </ScheduledContent>
      </ScheduledContents>
    </ScheduledFormatTemplate>
  </ScheduledFormatTemplates>
</root>

I'm not a developer.

I need to hack an XML file to duplicate hundreds of resources that are each assigned a GUID. Is there a way to parse the entire file, replacing each GUID found in a tag with a dynamically generated one?

Basically - every UniqueID tag (but not the ContentUniqueID tags) needs a new GUID.

<root xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
  <Name>[redacted]</Name>
  <UniqueId>7a136c33-3ea8-4f99-8f99-bbe411972203</UniqueId>
  <Enabled>true</Enabled>
  <Behavior>EmptyOnly</Behavior>
  <Subscriptions />
  <ScheduledFormatTemplates>
    <ScheduledFormatTemplate>
      <Name>[redacted]</Name>
      <UniqueId>1cfaba3e-bfd5-4d2f-a1df-14020ad2f7da</UniqueId>
      <ContentUniqueId>67c58741-fe1b-4c15-8dc0-8b4c01f6f18f</ContentUniqueId>
      <ScheduledContents>
        <ScheduledContent xsi:type="SFTR">
          <Name>[redacted]</Name>
          <UniqueId>b4a60646-b62b-43e7-b2a2-7d37875ab33f</UniqueId>
          <ContentUniqueId>ba634a97-9faf-4bfa-a9b4-d8a2475b82e6</ContentUniqueId>
          <ScheduledContents>
            <ScheduledContent>
              <Name>[redacted]</Name>
              <UniqueId>6f8e6e6c-1f94-4caa-8730-6859448138eb</UniqueId>
              <ContentUniqueId>938b0a24-4043-4a16-bc2d-25dbdb21a659</ContentUniqueId>
            </ScheduledContent>
          </ScheduledContents>
        </ScheduledContent>
      </ScheduledContents>
    </ScheduledFormatTemplate>
  </ScheduledFormatTemplates>
</root>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

上课铃就是安魂曲 2024-08-20 17:47:19

我不知道有这样的工具,但开发人员应该能够生成一个 powershell 脚本或 perl 脚本,在 10-15 分钟内完成此操作。

如果您发布 XML 文件的示例,我敢打赌有人甚至可能会发布工作代码。如果您将问题表述为挑战,就会得到更多人的回应。根据 XML 文件的复杂性,脚本可能只有 10 行。


如果还有其他要求,例如 - 如果您希望从特定范围中选择新的 GUID - 这仍然是可能且简单的,但您需要说明所有这些要求。


## 
## ReplaceGuids.ps1
##
## Reads an XML document, and emits an output doc.  The output replaces
## the Text value of each node in the input with LocalName="UniqueId", with 
## a new Guid.
##
## Thu, 10 Dec 2009  12:06
##


# Create the XmlReader  
$xr = [system.Xml.XmlReader]::Create("c:\data\Doug.xml")
# Create the XmlWriter
$sw = New-Object System.IO.StringWriter
$xw = New-Object System.Xml.XmlTextWriter $sw
$xw.Formatting = "indented"
$xw.Indentation = 2

$elementName = ""

# loop over each element in XmlReader
while ($true) {
  if ($xr.Read() -eq $false) { break; }
  switch ($xr.NodeType.ToString())
  {
    "Element" {
        $xw.WriteStartElement($xr.Name)
        $xw.WriteAttributes($xr, $false)
        if ($xr.IsEmptyElement) { $xw.WriteEndElement(); $elementName = ""; }
        else {$elementName = $xr.LocalName; }
    }

    "EndElement" {
        $xw.WriteEndElement()
        $elementName = ""
    } 

    "Text" {
      if ($elementName -eq "UniqueId") {
        $guid = [System.Guid]::NewGuid()
        $xw.WriteValue($guid.ToString())
      } else {
        $xw.WriteValue($xr.Value)
      }
    }
  }
}

$xr.Close()
$xw.Flush()
$sw.Flush()
Write-Output $sw.ToString()

I don't know of a tool like that, but a developer ought to be able to produce a powershell script, or a perl script, that does this in a matter of 10-15 minutes.

If you post the sample of the XML file, I'll bet someone might even post working code. If you phrase your Q as a challenge, you'll get more people to respond. Depending on the complexity of the XML file, it might be as few as 10 lines of script.


If there are other requirements, like - if you want the new GUIDs to all be selected from a particular range - that will still be possible and straightforward, but you'll need to state all those requirements.


## 
## ReplaceGuids.ps1
##
## Reads an XML document, and emits an output doc.  The output replaces
## the Text value of each node in the input with LocalName="UniqueId", with 
## a new Guid.
##
## Thu, 10 Dec 2009  12:06
##


# Create the XmlReader  
$xr = [system.Xml.XmlReader]::Create("c:\data\Doug.xml")
# Create the XmlWriter
$sw = New-Object System.IO.StringWriter
$xw = New-Object System.Xml.XmlTextWriter $sw
$xw.Formatting = "indented"
$xw.Indentation = 2

$elementName = ""

# loop over each element in XmlReader
while ($true) {
  if ($xr.Read() -eq $false) { break; }
  switch ($xr.NodeType.ToString())
  {
    "Element" {
        $xw.WriteStartElement($xr.Name)
        $xw.WriteAttributes($xr, $false)
        if ($xr.IsEmptyElement) { $xw.WriteEndElement(); $elementName = ""; }
        else {$elementName = $xr.LocalName; }
    }

    "EndElement" {
        $xw.WriteEndElement()
        $elementName = ""
    } 

    "Text" {
      if ($elementName -eq "UniqueId") {
        $guid = [System.Guid]::NewGuid()
        $xw.WriteValue($guid.ToString())
      } else {
        $xw.WriteValue($xr.Value)
      }
    }
  }
}

$xr.Close()
$xw.Flush()
$sw.Flush()
Write-Output $sw.ToString()
肤浅与狂妄 2024-08-20 17:47:19

此处有一个基于网络的工具可以执行此操作

There is a web-based tool that does this here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文