wanted xml tool
It looks like it's not working quite right. Sed seems to not be ignoring leading whitespace the way I thought it did. Leaving the '^' out of the first regular expression should fix that.
That would be in windows.
My sed fu is actually pretty weak.
That would be
Code: Select all
sed -n "/<ShipClass*/,/<\/ShipClass>/"p TranscendenceSource\*.xml extensions\*.xml > ships.txt
My sed fu is actually pretty weak.
- Mutos
- Militia Lieutenant
- Posts: 218
- Joined: Thu Aug 14, 2008 3:31 am
- Location: Near Paris, France
- Contact:
Hi all,
Code is longer in VBS ^-^
It's still crude as to how it skips comments. I had to do that because it fumbled on a tag-like string in comments in Transcendence.xml. What I really would want to do is not skip comments, but just be sure to not get a tag-like comment for a real tag. I got too far along the way. Also, there are checks I would like to implement to avoid hard crashes when something does not exist...
Code is longer in VBS ^-^
Code: Select all
' ***************************************************
'
' Created by Benoît ROBIN
' Date : 10/09/2008
' - Extract a type of XML tags to a target file
'
' ***************************************************
' ***************************************************
' Initialisation
' ***************************************************
' Any variable used must be declared explicitely
Option Explicit
' Constants
Const ForReading = 1
Const ForWriting = 2
Const sOpeningComments = "<!--"
Const sClosingComments = "-->"
Const sSeparatorLine = "<!-- ------------------------------------------------------------- -->"
' Dimension variables
Dim sSourceFileName, sTargetFileName, sTag
Dim sOpeningTag, sClosingTag
Dim sMessage, sUsage
Dim bWriteLine, bComments
Dim oSourceFileStream, oTargetFileStream
Dim sNextLine
Dim oFileSystem
Dim iReturnCode
Dim oArgs
' Create filesystem object
Set oFileSystem = CreateObject("Scripting.FileSystemObject")
'
' ***************************************************
' ***************************************************
' Check command-line argument
'
' 1st argument must be the source file name
' 2nd argument must be the target file name
' 3nd argument must be the tag
'
' Behaviour : outputs the tag contents
' from the source file
' to the target file
' 0 : source file exists
' 1 : source file doesn't exists
' 2 : incorrect # of arguments
' ***************************************************
' Default values
sUsage = "Usage : XML-Extract.vbs <Source File> <Target File> <Tag>"
iReturnCode = 0
' Get command-line arguments
Set oArgs = WScript.Arguments
' Check arguments number
If oArgs.Count<>3 Then
sMessage="Incorrect Arguments Number" & VbCrLf & sUsage
WScript.Echo(sMessage)
iReturnCode = 2
WScript.Quit(iReturnCode)
End If
' Parse arguments
sSourceFileName = oArgs(0)
sTargetFileName = oArgs(1)
sTag = oArgs(2)
'
' ***************************************************
' ***************************************************
' Check actual source file
' ***************************************************
If oFileSystem.FileExists(sSourceFileName) Then
sMessage = "File """ & sSourceFileName & """ exists"
Else
iReturnCode = 1
sMessage = "File """ & sSourceFileName & """ does not exists"
WScript.Echo(sMessage)
iReturnCode = 1
WScript.Quit(iReturnCode)
End If
'
' ***************************************************
' ***************************************************
' Loop on the source file lines
' ***************************************************
' At first we don't write anything on target file
bWriteLine = False
bComments = False
' Open source file for reading
Set oSourceFileStream = oFileSystem.OpenTextFile(sSourceFileName, ForReading, True)
' Open target file for writing : overwrite anything
' that might exist !
Set oTargetFileStream = oFileSystem.OpenTextFile(sTargetFileName, ForWriting, True)
' Prepare for getting tags
sOpeningTag = "<" & sTag & " "
sClosingTag = "</" & sTag
' Read source lines until EoF
Do Until oSourceFileStream.AtEndOfStream
' Read Line
sNextLine = oSourceFileStream.Readline
' Check if comments
If Not bComments Then
' Not comments : search for a beginning comment
If InStr(sNextLine, sOpeningComments)>0 Then
' Opening comment found : search for single-line comments
If InStr(sNextLine, sClosingComments)>0 Then
' Single-line comments : do not write but do not change comment flag
Else
' Multiple lines comments : do not write and change comment flag
bComments = True
End If
Else
' Not comment : check for tags
If Not bWriteLine Then
' Outside tag : search for opening tag
If InStr(sNextLine, sOpeningTag)>0 Then
' Opening tag found : change tag flag and write line
bWriteLine = True
oTargetFileStream.Writeline sSeparatorLine
oTargetFileStream.Writeline sNextLine
Else
' No opening tag found : do nothing
End If
Else
' Inside tag : write line and search for closing tag
oTargetFileStream.Writeline sNextLine
If InStr(sNextLine, sClosingTag)>0 Then
' Closing tag : toggle flag, write separator line and blank line
bWriteLine = False
oTargetFileStream.Writeline sSeparatorLine
oTargetFileStream.Writeline
Else
' Do nothing
End If
End If
End If
Else
' Already multiple lines comments : search for closing comments
If InStr(sNextLine, sClosingComments)>0 Then
' End fo MLC : do not write and change comment flag
bComments = False
End If
End If
Loop
' Close files
oSourceFileStream.Close
oTargetFileStream.Close
'
' ***************************************************
' ***************************************************
' Exit with the appropriate return code
' ***************************************************
WScript.Quit(iReturnCode)
'
' ***************************************************