views:

74

answers:

2

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'&gt;
  <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>
+2  A: 

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()
Cheeso
No other requirements really. They just need to be distinct from each other. I thought maybe this was something people had to do frequently.
Doug Chase
I'll post some sample code. Essentially, every <uniqueID> tag in the document should be replaced with a new, distinct GUID.
Doug Chase
I do speak Sysadmin PowerShell but not Developer PowerShell, so if that's a nice way to accomplish this I'll be able to follow what you're telling me.
Doug Chase
Wow Cheeso, thanks. I will give this a try now.
Doug Chase
My new document is exactly twice as large as my old one. Looking into why this might be now.
Doug Chase
I haven't looked closely yet, but just for fun I plugged the file into my app and it looks like it's working great so far! Thanks so much for the great answer and viva StackOverflow.
Doug Chase
A: 

There is a web-based tool that does this here

BigJoe714
Nevermind, I dont this this will work for you. I replaces ALL Guids that it finds.
BigJoe714