N
N
Natasha0002022-01-11 14:57:51
cmd/bat
Natasha000, 2022-01-11 14:57:51

How to split xml file using bat/cmd?

Help me please !
Need to split xml file

<?xml version="1.0" encoding="UTF-8"?>
<post>
    <OrderReference>313</OrderReference>
    <OrderDate>2021-12-20 15:21:26</OrderDate>
    <ItemList>
      <ListLineItem>
        <OrderPosNumber>1</OrderPosNumber>
        <DistributorsArticleNo>100-027</DistributorsArticleNo>
        <OrderQuantity>3</OrderQuantity>
        <price>4,00</price>
      </ListLineItem>
      <ListLineItem>
        <OrderPosNumber>2</OrderPosNumber>
        <DistributorsArticleNo>100-015</DistributorsArticleNo>
        <OrderQuantity>2</OrderQuantity>
        <price>2,99</price>
      </ListLineItem>
    </ItemList>
</post>
<post>
    <OrderReference>315</OrderReference>
    <OrderDate>2021-12-25 15:21:26</OrderDate>
    <ItemList>
      <ListLineItem>
        <OrderPosNumber>1</OrderPosNumber>
        <DistributorsArticleNo>100-007</DistributorsArticleNo>
        <OrderQuantity>2</OrderQuantity>
        <price>6,00</price>
      </ListLineItem>
      <ListLineItem>
        <OrderPosNumber>2</OrderPosNumber>
        <DistributorsArticleNo>100-005</DistributorsArticleNo>
        <OrderQuantity>3</OrderQuantity>
        <price>1,99</price>
      </ListLineItem>
    </ItemList>
</post>


on files by post tags:

1.xml
<?xml version="1.0" encoding="UTF-8"?>
<post>
    <OrderReference>313</OrderReference>
    <OrderDate>2021-12-20 15:21:26</OrderDate>
    <ItemList>
      <ListLineItem>
        <OrderPosNumber>1</OrderPosNumber>
        <DistributorsArticleNo>100-027</DistributorsArticleNo>
        <OrderQuantity>3</OrderQuantity>
        <price>4,00</price>
      </ListLineItem>
      <ListLineItem>
        <OrderPosNumber>2</OrderPosNumber>
        <DistributorsArticleNo>100-015</DistributorsArticleNo>
        <OrderQuantity>2</OrderQuantity>
        <price>2,99</price>
      </ListLineItem>
    </ItemList>
</post>


2.xml
<?xml version="1.0" encoding="UTF-8"?>
<post>
    <OrderReference>315</OrderReference>
    <OrderDate>2021-12-25 15:21:26</OrderDate>
    <ItemList>
      <ListLineItem>
        <OrderPosNumber>1</OrderPosNumber>
        <DistributorsArticleNo>100-007</DistributorsArticleNo>
        <OrderQuantity>2</OrderQuantity>
        <price>6,00</price>
      </ListLineItem>
      <ListLineItem>
        <OrderPosNumber>2</OrderPosNumber>
        <DistributorsArticleNo>100-005</DistributorsArticleNo>
        <OrderQuantity>3</OrderQuantity>
        <price>1,99</price>
      </ListLineItem>
    </ItemList>
</post>


etc.
The number of files is equal to the number of post

If this is not possible in BAT/CMD , please suggest a simple working program for this.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
wisgest, 2022-01-12
@wisgest

If the file looks like this, then it's simple (blank lines, if any, will be lost):

@echo off
setlocal enableextensions enabledelayedexpansion

:0
if "%~1" == "" (
  set /p in="Входной файл: "
  call :0 !in!
  exit /b
)

if not exist "%~1" (
  echo "%~f1" не найден.
  endlocal
  exit /b 1
)

set /a i = 0
for /f "usebackq skip=1 delims=" %%L in ("%~1") do  (
  if "%%L" == "<post>"  (
    set /a i += 1
    (echo ^<?xml version="1.0" encoding="UTF-8"?^>)>!i!.xml
  )
  (echo(%%L)>>!i!.xml
)
endlocal

- The XML format is not taken into account, there is a division by lines <post>, which must start from the beginning of the line and not have spaces at the end of the line.

R
res2001, 2022-01-11
@res2001

It is not realistic to process xml in batch files, because tag symbols are service for cmd, as soon as such a line gets into the command line in the batch file there will be an error.
So discard this option immediately.
Windows has other scripting languages ​​out of the box, like power shell or jscript, use one of them.
On jscript, in addition to the fact that you can simply process xml as a text file, you can also use an xml parser with all the goodies. I don’t know about the power shell, maybe there is something similar.
Since you are using wordpress, then PHP is already installed, you can use it.

S
Saboteur, 2022-01-12
@saboteur_kiev

If you have bash at hand, or if git-bash is on the system, then you can use the following script for bash.

NL="
"
while read; do
  if [ "$REPLY" == "<post>" ]; then
    BLOCK='<?xml version="1.0" encoding="UTF-8"?>'
  elif ; then
    order="${REPLY#*Reference>}"
    order="${order%</Order*}"
  fi
  BLOCK="${BLOCK}${NL}${REPLY}"

  if [ "$REPLY" == "</post>" ]; then
    echo "$BLOCK">$order.xml
    echo "Writing block to $order"
  fi
done<"${1}"

call
bash myscript MYFILE.xml
get files.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question