Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2017
    Thanked 0 Times in 0 Posts

    Copying parts of text from various XML files and inserting into certain spots into a single XML file

    Hello everyone!

    I am going through 9000+ XML files one by one copying various text and inserting the copied text into certain locations within a new single XML file.

    This is proving to be very, very time consuming to do manually (I could possibly be done by next year ).

    I read that I could possibly automate this process using Powershell (which I have never used) so I started researching and it looks like I have a lot to learn.

    Here is some example code from one of the XML files:

    <?xml version="1.0" encoding="utf-8"?>
    <object hash="256A1FF9" def="ark">
    <field name="disLibItemId" type="Id64">7792483006679</field>
    <field hash="B435B769" type="String">entityarchetypeslibrary\7792483006679.ark.fcb</field>
    <field hash="2D90D933" type="Hash64">85493604E8E717A1</field>
    <field name="Name" type="String">Ruins_Chorten_01</field>
    <field hash="6EA390DB" type="String">fcc_graphics:single:sub-thematic:ruins:buildings:ruins_chorten_01</field>

    There is more code in the file but irrelevant to this question.

    The lines which have the information I am copying are Lines 3, 6 and 7

    And here is what I am trying to accomplish:

    <Entry Id="TEXT FROM LINE 7 BETWEEN "String"> AND </field> HERE" Display="TEXT FROM LINE 6 BETWEEN "String"> AND </field> HERE" SourceType="0" Sync="True" ObjectCost="0" ObjectType="0" ArchetypeId="TEXT FROM LINE 3 BETWEEN "Id64"> AND </field> HERE" />
    Taking the example above the result is:

    <Entry Id="fcc_graphics:singleplayer:sub-thematic:ruins:buildings:ruins_chorten_01" Display="Ruins_Chorten_01" SourceType="0" Sync="True" ObjectCost="0" ObjectType="0" ArchetypeId="7792483006679"/>
    And so it goes until I have 9000+ lines in my single XML file.

    Some helpful info:

    • Every XML file begins just as I've shown above, the only content that changes is between the tags I have specified above.
    • The length of the text I am copying from lines 3, 6 and 7 varies.
    • The text I am copying from line 7 may have random symbols in it such as ;:'\ / separating the words
    • The directory with the 9000+ XML files is C:\archetypeslibrary
    • The above directory has various named XML files
    • The location of the "single" XML file I am copying the text to is in C:\archetypeslibrary_singlefile\obj_inventory.xml

    In an ideal situation, I would be able to write some code that accomplishes this task but being new to Powershell, I immediately realized I had a lot of searching to do to start learning and that the above is way over my head so I figured I could find some code that extracts (for example..Line 3 from every file to a single xml file in order) I could then use the Notepad++ "column editor" option to delete what I didn't want in the lines and insert them into my new single file.

    After days of searching on how to accomplish this particular task, I ran across a post here on these forums where RetiredGeek posted some code "kind of" similar to this but it doesn't extract the entire line. That post is located here:

    He posted the following code which I have played with but have not had success just yet:

    Param (
    	   [string] $FullyQualifiedPath
    #Note: Make sure the following path is outside those being searched!
    $OutputFile = "G:\Test\Results\BegEndResults.txt"  #Adjust as needed
    $Pat1 = [regex] '(Begin)*(End)'  #Adjust tags between () as appropriate
    Remove-Item "$OutputFile"
    Get-ChildItem -Path "$FullyQualifiedPath" |
    Get-Content  | Select-String -Pattern $Pat1 -AllMatches >> "$OutputFile"
    Could anyone possibly offer some advice on how to accomplish this?

    Thank you for any assistance!
    Last edited by Markstein; 2017-07-13 at 01:14.

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Thanked 1,095 Times in 1,018 Posts
    PowerShell allows you to manipulate XML directly.
    You can search for nodes with Select-XML.
    You can write XML. $XmlWriter = New-Object System.XMl.XmlTextWriter($XMLfileName,$Null)

    cheers, Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts