Answer the question
In order to leave comments, you need to log in
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>
<?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>
<?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>
Answer the question
In order to leave comments, you need to log in
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
<post>
, which must start from the beginning of the line and not have spaces at the end of the line.
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.
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}"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question