0

テキスト ファイルを特定の xml 形式に解析する必要があります。

ABC-DATA-FILE-VERSION: 2.0

OBJFILE:    /home/abc/src/solaris/abc.o

TIMESTAMP: 1348314377 727216

SRCFILE:    /home/abc/src/solaris/abc.C

    167 7
    170 7
    174 0
    179 0
    174 0
    192 7
    196 7
    199 7
    215 0

OBJFILE:    /home/abcd/src/solaris/abcd.o

TIMESTAMP: 1348314377 727216

SRCFILE:    /home/abcd/src/solaris/abcd.C

    58  7
    65  7
    66  7
    67  7
    69  0
    79  0
    84  0
    97  14
    100 7
    108 14
    110 7
    115 14

OBJFILE:    /home/abcd/src/solaris/xyz.o

TIMESTAMP: 1348314377 727216

SRCFILE:    /home/abcd/src/solaris/xyz.C

    978   0
    979   1
    993   0
    996   0
    997   0
    1011  0
    1003  0
    1004  0
    1011  0

今、私はそれを特定のxmlファイル形式に変換したかった. お気に入り

<packages>
    <package name="com" line-rate="0.45161290322580644" branch-rate="0.4915254237288136" complexity="3.391891891891892">
        <classes>
            <class branch-rate="0" complexity="0" filename="/home/abcd/src/solaris/abcd.C" line-rate="0.25" name="TestRunnerModel">
                <methods/>
                <lines>
                    <line number="13" hits="1" branch="true"/>
                    <line number="14" hits="1" branch="true"/>
                    <line number="15" hits="1" branch="false"/>
                    <line number="12" hits="0" branch="false"/>
                </lines>
            </class>
            <class branch-rate="0" complexity="0" filename="/home/abcd/src/solaris/abcd.C" line-rate="0.25" name="TestRunnerModel">
                <methods/>
                <lines>
                    <line number="13" hits="1" branch="true"/>
                    <line number="14" hits="1" branch="true"/>
                    <line number="15" hits="1" branch="false"/>
                    <line number="12" hits="0" branch="false"/>
                </lines>
            </class>
            <class branch-rate="0" complexity="0" filename="/home/abcd/src/solaris/xyz.C" line-rate="0.25" name="TestRunnerModel">
                <methods/>
                <lines>
                    <line number="13" hits="1" branch="true"/>
                    <line number="14" hits="0" branch="true"/>
                    <line number="15" hits="1" branch="false"/>
                    <line number="12" hits="0" branch="false"/>
                </lines>
            </class>
        </classes>
    </package>
</packages>

ほとんどの xml パラメータは一定で、次のように入力する必要があるのはごくわずかです。

SRCFILE から読み取る FILENAME: /home/abcd/src/solaris/xyz.C および

line number="978" hits="0" branch="true" line number="979" hits="1" branch="false" など

等々。助けてください。

4

1 に答える 1

0

原則として、それは非常に簡単です。特定の入力形式で入力があり、特定の出力形式で出力を生成したいと考えています。入力形式の構造を識別し、その構造を表すデータ構造を構築するには、入力形式のパーサーが必要です。また、必要な XML を生成するデータ構造用のシリアライザーも必要です。

入力形式用の解析ライブラリが存在する場合があります。その場合、独自のパーサーを最初から作成する代わりに、解析ライブラリを使用することをお勧めします。あなたの言語には、物事を XML としてシリアライズするためのライブラリもあるかもしれません (おそらくあるでしょう)。それらを使用したい場合があります。

定義された形式のパーサーを作成する方法を知っていれば、何をする必要があるかがわかります。そうでない場合は、sed、awk、perl、または選択したバッチ エディターを使用して偽造できる可能性がありますが、構文解析について学ぶことに時間を費やすと、プログラマーとしての生活がより楽しくなります。

于 2012-09-25T15:25:45.880 に答える