XML to tab

Print as transpose if data is too wide to view from left to right.

Python, regex experiment.

# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import re
# xml2Tab_t.py drill4Opseq.xml 20 ProductData Machine MachiningData SequenceOperation > hasil.txt

# note:
# NOf = NumberOf
# [0]funtionName, [1]XML, [2]maxNumOfColumn, [3]wantedTag

minNOfArgv = 3
maxNOfColumn = int(sys.argv[minNOfArgv-1])

# --------------------------------

def printAs(type, line):
    if type=="title":
        doPrint("\s", "=", line)
    elif type=="data":
        doPrint("\"", "\"",  line)

def storeAs(dType, tType, line, array):
    if dType=="title":
        line = re.sub(r"^\s+", "", line)
        beg = "\s"
        en = "="        
    elif dType=="data":
        beg = "\""
        en = "\""

    reg = beg + r"(.*?)" + en
    i=0
    j=0

    if tType!="transpose" and tType!="normal":
        nResult = len(re.findall(reg, line, re.S))
        if nResult < maxNOfColumn:
            if nResult!=0:
                tType="normal"            
        else:
            tType="transpose"          
                
    for match in re.finditer(reg, line, re.S):        
        if tType=="transpose":            
            if dType=="title":
                array.append([]) #
                array[i].append(match.group(1))
                i=i+1
            elif dType=="data":
                array[i].append(match.group(1))
                i=i+1
        elif tType=="normal":
            if dType=="title":
                if len(array)==0:
                    array.append([]) #
                array[0].append(match.group(1))
            elif dType=="data":
                if i==0:
                    array.append([])
                    i=len(array)
                array[i-1].append(match.group(1))            
def doPrint(beg, en, line):    
    reg = beg + r"(.*?)" + en
        
    "remove whitespace @beg.of line, esp. for title"
    line = re.sub(r"^\s+", "", line)

    if re.search(reg, line):  #biar bisa \n di luar for loop
            "print substring between beg and end"
        for match in re.finditer(reg, line, re.S):
            print(match.group(1), end="")
            print("\t", end="")
        print("")

def printArray(type, array):     
    for i in xrange(0, len(array)):
        for j in xrange(0, len(array[i])):
            print(str(array[i][j]), end="")
            print("\t", end="")
        print("")
    print("")

def main():    
    iLine = 0              # index line
    cArg = 0               # current (observed) argument
    insideTag = False
    aData = []
    
    if len(sys.argv) < minNOfArgv + 1:
        # note: sys.argv[0] is function name
        return    
        fo = open(sys.argv[1])    
    for sLine in fo:
        
        "check boundary (e.g., tag "
        for a in xrange(0, len(sys.argv) - minNOfArgv):
            reg = r"\<" + sys.argv[a + minNOfArgv]
            if re.search(reg, sLine):
                insideTag = True
                cArg = a
                iLine=1                
            reg = r"\<\/" + sys.argv[a + minNOfArgv]
            if re.search(reg, sLine):
                insideTag = False
                if sys.argv[cArg + minNOfArgv]=="ProductData":
                    print("")
                else:
                    print("<" + sys.argv[cArg + minNOfArgv]+">")
                    printArray("", aData)

        "get data"
        if insideTag:
            if sys.argv[cArg + minNOfArgv]=="ProductData":
                if iLine==2:
                    print("<"+sys.argv[cArg + minNOfArgv]+">")
                    printAs("title", sLine)
                    printAs("data", sLine)
                    print("")
                elif iLine==3:
                    printAs("title", sLine)
                    printAs("data", sLine)
                else:
                    printAs("data", sLine)
            else:
                if iLine==2:
                    aData[:]=[]
                    storeAs("title", "", sLine, aData)
                    storeAs("data", "", sLine, aData)
                else:
                    storeAs("data", "", sLine, aData)
                    
            iLine = iLine + 1    
    fo.close()    

if __name__ == "__main__":
    main()

Print inside tag XML

Regex, python

1. interface .bat (double click), e.g., execute.bat

main.py drill4Opseq.xml tag1 tag2 > hasil.txt

2. python script, e.g., main.py

# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import re

def printData(beg, en, line):    
    reg = beg + r"(.*?)" + en
        
    "remove whitespace @beg.of line, esp. for title"
    line = re.sub(r"^\s+", "", line)

    if re.search(reg, line):    
        "print substring between beg and end"
        for match in re.finditer(reg, line, re.S):
            print(match.group(1), end="")
            print("\t", end="")
        print("")

def main():
    i = 0
    arg = 0
    insideTag = False
    #app = QtGui.QApplication(sys.argv)
    if len(sys.argv)")
                printData("\s", "=", line)
            printData("\"", "\"",  line)
            i=i+1
    fh.close()    

if __name__ == "__main__":
    main()

ps: pakai &nbsp; buat indent 😀