批处理脚本捕获其子元素具有CDATA并输出到xml文件的所有元素

时间:2013-09-11 13:44:30

标签: xml batch-file xml-parsing

我正在寻找一个批处理脚本,可以将其子元素的所有父元素复制到xml文件中并输出到新的xml文件中。{/ p>

这是我的xml文件:

<![CDATA[

因此,预期输出必须捕获其子元素具有]]> ... <?xml version="1.0" encoding="UTF-8" standalone="no"?> <books xmlns:jndi="urn:jboss:jndi-binding-service:1.0"> <jndi:bindings> <jndi:binding name="property/category/books/book1"> <jndi:value type="java.lang.String"> <![CDATA[ <?xml version="1.0" encoding="ISO-8859-1"?> <urlConfig> <defaults catID="1983" subcatID="1987" method="get" onError="keep"/> <urlKey name="logo" altURL="def.com"> <address>abc.com</address> </urlKey> <urlKey name="logo1" altURL="def.com"> <address>abc.com</address> </urlKey> </urlConfig> ]]> </jndi:value> </jndi:binding> <jndi:binding name="property/category/books/book2"> <jndi:value type="java.lang.String"> abc.com </jndi:value> </jndi:binding> <jndi:binding name="property/category/books/book3"> <jndi:value type="java.lang.String"> <![CDATA[ <?xml version="1.0" encoding="ISO-8859-1"?> <urlConfig> <defaults catID="1989" subcatID="1987" method="get" onError="keep"/> <urlKey name="logo" altURL="def.com"> <address>abc.com</address> </urlKey> <urlKey name="logo1" altURL="def.com"> <address>abc.com</address> </urlKey> </urlConfig> ]]> </jndi:value> </jndi:binding> <jndi:binding name="property/category/books/book4"> <jndi:value type="java.lang.String"> <![CDATA[ <?xml version="1.0" encoding="ISO-8859-1"?> <urlConfig> <defaults catID="1990" subcatID="1987" method="get" onError="keep"/> <urlKey name="logo" altURL="def.com"> <address>abc.com</address> </urlKey> <urlKey name="logo1" altURL="def.com"> <address>abc.com</address> </urlKey> </urlConfig> ]]> </jndi:value> </jndi:binding> <jndi:binding name="property/category/books/book5"> <jndi:value type="java.lang.String"> abc.com </jndi:value> </jndi:binding> </jndi:bindings> </books> 的所有父元素。在此示例中,父元素为<![CDATA[

预期输出为:output.xml

]]>

我决定使用xml处理器,xslt来处理xml文件但是如果文件中包含CDATA部分,xslt处理器就不容易处理xml文件。

1 个答案:

答案 0 :(得分:0)

这可能会对您有所帮助:

@ECHO OFF &SETLOCAL
set "infile=file.xml"
set "outfile=file.new"
rem IMPORTANT! Put a [tab] and a [space] behind the colon to the delims.
for /f "tokens=1-3delims=<>:     " %%a in ('findstr /nc:"jndi:binding" /c:"![CDATA[" /c:"/jndi:binding" "%infile%"') do (
    if "%%~b:%%~c"=="jndi:binding" set "start=%%a"
    if "%%~b"=="![CDATA[" set "found=%%a"
    if "%%~b:%%~c"=="/jndi:binding" (
        if defined found call set "line=%%line%%%%start%%,%%ap;"
        set "start="
        set "found="
    )
)
sed -n "%line%" "%infile%">"%outfile%"
type "%outfile%"

sed for Windows