Identify empty xml files?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a requirement to identify and write all xml file names which are empty to a text file for reporting purpose. Empty , here means the xml file has the usual header tag <?xml version="1.0" encoding="UTF-8"?>
followed by an empty open and close tag.
Sample file:
1)
<?xml version="1.0" encoding="UTF-8"?>
<STBTests>
</STBTests>
2)
<?xml version="1.0" encoding="UTF-8"?>
<UMTTests>
</UMTTests>
There are no data in the xml files apart from this. Any suggestions on how to approach this would be great.
find xml
 |Â
show 2 more comments
up vote
1
down vote
favorite
I have a requirement to identify and write all xml file names which are empty to a text file for reporting purpose. Empty , here means the xml file has the usual header tag <?xml version="1.0" encoding="UTF-8"?>
followed by an empty open and close tag.
Sample file:
1)
<?xml version="1.0" encoding="UTF-8"?>
<STBTests>
</STBTests>
2)
<?xml version="1.0" encoding="UTF-8"?>
<UMTTests>
</UMTTests>
There are no data in the xml files apart from this. Any suggestions on how to approach this would be great.
find xml
Can you be specific about how you define an "empty" file? Is it (at most) one tag on the first line, followed by pairs of open and close tags with nothing but whitespace between them?
â Sparhawk
Mar 15 at 20:56
How about file size, it could be a good tell if non-empty files are bigger, usefind /path -size -128c
to find files with less than 128 bytes
â Dalvenjia
Mar 15 at 21:06
other option is to count the lines,for F in *.xml; do if [ $(wc -l "$F") -lt 4 ]; then echo "$F"; fi; done
â Dalvenjia
Mar 15 at 21:11
1
@Dalvenjia: very bad idea. Think of an inline node like<foo>xxxxxxxxxxxxxxxxxxxxxxxx</foo>
â Gilles Quenot
Mar 15 at 21:44
What about comments and processing instructions?
â ErikF
Mar 15 at 23:07
 |Â
show 2 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a requirement to identify and write all xml file names which are empty to a text file for reporting purpose. Empty , here means the xml file has the usual header tag <?xml version="1.0" encoding="UTF-8"?>
followed by an empty open and close tag.
Sample file:
1)
<?xml version="1.0" encoding="UTF-8"?>
<STBTests>
</STBTests>
2)
<?xml version="1.0" encoding="UTF-8"?>
<UMTTests>
</UMTTests>
There are no data in the xml files apart from this. Any suggestions on how to approach this would be great.
find xml
I have a requirement to identify and write all xml file names which are empty to a text file for reporting purpose. Empty , here means the xml file has the usual header tag <?xml version="1.0" encoding="UTF-8"?>
followed by an empty open and close tag.
Sample file:
1)
<?xml version="1.0" encoding="UTF-8"?>
<STBTests>
</STBTests>
2)
<?xml version="1.0" encoding="UTF-8"?>
<UMTTests>
</UMTTests>
There are no data in the xml files apart from this. Any suggestions on how to approach this would be great.
find xml
edited Mar 15 at 20:54
Sparhawk
8,32863487
8,32863487
asked Mar 15 at 20:52
Kavin Palaniswamy
204
204
Can you be specific about how you define an "empty" file? Is it (at most) one tag on the first line, followed by pairs of open and close tags with nothing but whitespace between them?
â Sparhawk
Mar 15 at 20:56
How about file size, it could be a good tell if non-empty files are bigger, usefind /path -size -128c
to find files with less than 128 bytes
â Dalvenjia
Mar 15 at 21:06
other option is to count the lines,for F in *.xml; do if [ $(wc -l "$F") -lt 4 ]; then echo "$F"; fi; done
â Dalvenjia
Mar 15 at 21:11
1
@Dalvenjia: very bad idea. Think of an inline node like<foo>xxxxxxxxxxxxxxxxxxxxxxxx</foo>
â Gilles Quenot
Mar 15 at 21:44
What about comments and processing instructions?
â ErikF
Mar 15 at 23:07
 |Â
show 2 more comments
Can you be specific about how you define an "empty" file? Is it (at most) one tag on the first line, followed by pairs of open and close tags with nothing but whitespace between them?
â Sparhawk
Mar 15 at 20:56
How about file size, it could be a good tell if non-empty files are bigger, usefind /path -size -128c
to find files with less than 128 bytes
â Dalvenjia
Mar 15 at 21:06
other option is to count the lines,for F in *.xml; do if [ $(wc -l "$F") -lt 4 ]; then echo "$F"; fi; done
â Dalvenjia
Mar 15 at 21:11
1
@Dalvenjia: very bad idea. Think of an inline node like<foo>xxxxxxxxxxxxxxxxxxxxxxxx</foo>
â Gilles Quenot
Mar 15 at 21:44
What about comments and processing instructions?
â ErikF
Mar 15 at 23:07
Can you be specific about how you define an "empty" file? Is it (at most) one tag on the first line, followed by pairs of open and close tags with nothing but whitespace between them?
â Sparhawk
Mar 15 at 20:56
Can you be specific about how you define an "empty" file? Is it (at most) one tag on the first line, followed by pairs of open and close tags with nothing but whitespace between them?
â Sparhawk
Mar 15 at 20:56
How about file size, it could be a good tell if non-empty files are bigger, use
find /path -size -128c
to find files with less than 128 bytesâ Dalvenjia
Mar 15 at 21:06
How about file size, it could be a good tell if non-empty files are bigger, use
find /path -size -128c
to find files with less than 128 bytesâ Dalvenjia
Mar 15 at 21:06
other option is to count the lines,
for F in *.xml; do if [ $(wc -l "$F") -lt 4 ]; then echo "$F"; fi; done
â Dalvenjia
Mar 15 at 21:11
other option is to count the lines,
for F in *.xml; do if [ $(wc -l "$F") -lt 4 ]; then echo "$F"; fi; done
â Dalvenjia
Mar 15 at 21:11
1
1
@Dalvenjia: very bad idea. Think of an inline node like
<foo>xxxxxxxxxxxxxxxxxxxxxxxx</foo>
â Gilles Quenot
Mar 15 at 21:44
@Dalvenjia: very bad idea. Think of an inline node like
<foo>xxxxxxxxxxxxxxxxxxxxxxxx</foo>
â Gilles Quenot
Mar 15 at 21:44
What about comments and processing instructions?
â ErikF
Mar 15 at 23:07
What about comments and processing instructions?
â ErikF
Mar 15 at 23:07
 |Â
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Try this using xmllint with a xpath expression :
#!/bin/sh
for xml in *.xml; do
bool=$(xmllint --xpath 'count(//*)=1 and string-length(//*[1])=1' "$xml")
if [ $bool = true ]; then
echo "$xml" >> xml_list_files
fi
done
cat xml_list_files
The expression test that the file have only one node without any text content. In this case, the command return true
This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
â Kavin Palaniswamy
Mar 16 at 0:19
Thanks. Don't forget to accept/vote up if it fit your needs :)
â Gilles Quenot
Mar 16 at 0:22
1
Accepted and up voted! Thanks again ;)
â Kavin Palaniswamy
Mar 16 at 0:23
I had fun finding the solution, not that easy at a first glance
â Gilles Quenot
Mar 16 at 0:24
1
Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
â Kavin Palaniswamy
Mar 16 at 0:28
 |Â
show 1 more comment
up vote
0
down vote
to identify and write all xml file names which are empty to a text
file for reporting purpose
find
+ xmlstarlet
solution:
find . -type f -name "*.xml" -exec bash -c
'v=$(xmlstarlet sel -t -i "count(//*)=1 and //*[1][not(normalize-space())]" -o 1 -b $1);
[[ -n "$v" ]] && echo "$1" >> "empty_xml.txt"' _ ;
empty_xml.txt
file should contain a list of needed filenames/filepaths
Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
â Kavin Palaniswamy
Mar 16 at 0:22
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Try this using xmllint with a xpath expression :
#!/bin/sh
for xml in *.xml; do
bool=$(xmllint --xpath 'count(//*)=1 and string-length(//*[1])=1' "$xml")
if [ $bool = true ]; then
echo "$xml" >> xml_list_files
fi
done
cat xml_list_files
The expression test that the file have only one node without any text content. In this case, the command return true
This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
â Kavin Palaniswamy
Mar 16 at 0:19
Thanks. Don't forget to accept/vote up if it fit your needs :)
â Gilles Quenot
Mar 16 at 0:22
1
Accepted and up voted! Thanks again ;)
â Kavin Palaniswamy
Mar 16 at 0:23
I had fun finding the solution, not that easy at a first glance
â Gilles Quenot
Mar 16 at 0:24
1
Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
â Kavin Palaniswamy
Mar 16 at 0:28
 |Â
show 1 more comment
up vote
2
down vote
accepted
Try this using xmllint with a xpath expression :
#!/bin/sh
for xml in *.xml; do
bool=$(xmllint --xpath 'count(//*)=1 and string-length(//*[1])=1' "$xml")
if [ $bool = true ]; then
echo "$xml" >> xml_list_files
fi
done
cat xml_list_files
The expression test that the file have only one node without any text content. In this case, the command return true
This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
â Kavin Palaniswamy
Mar 16 at 0:19
Thanks. Don't forget to accept/vote up if it fit your needs :)
â Gilles Quenot
Mar 16 at 0:22
1
Accepted and up voted! Thanks again ;)
â Kavin Palaniswamy
Mar 16 at 0:23
I had fun finding the solution, not that easy at a first glance
â Gilles Quenot
Mar 16 at 0:24
1
Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
â Kavin Palaniswamy
Mar 16 at 0:28
 |Â
show 1 more comment
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Try this using xmllint with a xpath expression :
#!/bin/sh
for xml in *.xml; do
bool=$(xmllint --xpath 'count(//*)=1 and string-length(//*[1])=1' "$xml")
if [ $bool = true ]; then
echo "$xml" >> xml_list_files
fi
done
cat xml_list_files
The expression test that the file have only one node without any text content. In this case, the command return true
Try this using xmllint with a xpath expression :
#!/bin/sh
for xml in *.xml; do
bool=$(xmllint --xpath 'count(//*)=1 and string-length(//*[1])=1' "$xml")
if [ $bool = true ]; then
echo "$xml" >> xml_list_files
fi
done
cat xml_list_files
The expression test that the file have only one node without any text content. In this case, the command return true
edited Mar 15 at 23:02
answered Mar 15 at 21:30
Gilles Quenot
15.3k13448
15.3k13448
This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
â Kavin Palaniswamy
Mar 16 at 0:19
Thanks. Don't forget to accept/vote up if it fit your needs :)
â Gilles Quenot
Mar 16 at 0:22
1
Accepted and up voted! Thanks again ;)
â Kavin Palaniswamy
Mar 16 at 0:23
I had fun finding the solution, not that easy at a first glance
â Gilles Quenot
Mar 16 at 0:24
1
Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
â Kavin Palaniswamy
Mar 16 at 0:28
 |Â
show 1 more comment
This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
â Kavin Palaniswamy
Mar 16 at 0:19
Thanks. Don't forget to accept/vote up if it fit your needs :)
â Gilles Quenot
Mar 16 at 0:22
1
Accepted and up voted! Thanks again ;)
â Kavin Palaniswamy
Mar 16 at 0:23
I had fun finding the solution, not that easy at a first glance
â Gilles Quenot
Mar 16 at 0:24
1
Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
â Kavin Palaniswamy
Mar 16 at 0:28
This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
â Kavin Palaniswamy
Mar 16 at 0:19
This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
â Kavin Palaniswamy
Mar 16 at 0:19
Thanks. Don't forget to accept/vote up if it fit your needs :)
â Gilles Quenot
Mar 16 at 0:22
Thanks. Don't forget to accept/vote up if it fit your needs :)
â Gilles Quenot
Mar 16 at 0:22
1
1
Accepted and up voted! Thanks again ;)
â Kavin Palaniswamy
Mar 16 at 0:23
Accepted and up voted! Thanks again ;)
â Kavin Palaniswamy
Mar 16 at 0:23
I had fun finding the solution, not that easy at a first glance
â Gilles Quenot
Mar 16 at 0:24
I had fun finding the solution, not that easy at a first glance
â Gilles Quenot
Mar 16 at 0:24
1
1
Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
â Kavin Palaniswamy
Mar 16 at 0:28
Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
â Kavin Palaniswamy
Mar 16 at 0:28
 |Â
show 1 more comment
up vote
0
down vote
to identify and write all xml file names which are empty to a text
file for reporting purpose
find
+ xmlstarlet
solution:
find . -type f -name "*.xml" -exec bash -c
'v=$(xmlstarlet sel -t -i "count(//*)=1 and //*[1][not(normalize-space())]" -o 1 -b $1);
[[ -n "$v" ]] && echo "$1" >> "empty_xml.txt"' _ ;
empty_xml.txt
file should contain a list of needed filenames/filepaths
Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
â Kavin Palaniswamy
Mar 16 at 0:22
add a comment |Â
up vote
0
down vote
to identify and write all xml file names which are empty to a text
file for reporting purpose
find
+ xmlstarlet
solution:
find . -type f -name "*.xml" -exec bash -c
'v=$(xmlstarlet sel -t -i "count(//*)=1 and //*[1][not(normalize-space())]" -o 1 -b $1);
[[ -n "$v" ]] && echo "$1" >> "empty_xml.txt"' _ ;
empty_xml.txt
file should contain a list of needed filenames/filepaths
Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
â Kavin Palaniswamy
Mar 16 at 0:22
add a comment |Â
up vote
0
down vote
up vote
0
down vote
to identify and write all xml file names which are empty to a text
file for reporting purpose
find
+ xmlstarlet
solution:
find . -type f -name "*.xml" -exec bash -c
'v=$(xmlstarlet sel -t -i "count(//*)=1 and //*[1][not(normalize-space())]" -o 1 -b $1);
[[ -n "$v" ]] && echo "$1" >> "empty_xml.txt"' _ ;
empty_xml.txt
file should contain a list of needed filenames/filepaths
to identify and write all xml file names which are empty to a text
file for reporting purpose
find
+ xmlstarlet
solution:
find . -type f -name "*.xml" -exec bash -c
'v=$(xmlstarlet sel -t -i "count(//*)=1 and //*[1][not(normalize-space())]" -o 1 -b $1);
[[ -n "$v" ]] && echo "$1" >> "empty_xml.txt"' _ ;
empty_xml.txt
file should contain a list of needed filenames/filepaths
edited Mar 15 at 22:41
answered Mar 15 at 22:35
RomanPerekhrest
22.4k12144
22.4k12144
Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
â Kavin Palaniswamy
Mar 16 at 0:22
add a comment |Â
Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
â Kavin Palaniswamy
Mar 16 at 0:22
Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
â Kavin Palaniswamy
Mar 16 at 0:22
Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
â Kavin Palaniswamy
Mar 16 at 0:22
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430487%2fidentify-empty-xml-files%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Can you be specific about how you define an "empty" file? Is it (at most) one tag on the first line, followed by pairs of open and close tags with nothing but whitespace between them?
â Sparhawk
Mar 15 at 20:56
How about file size, it could be a good tell if non-empty files are bigger, use
find /path -size -128c
to find files with less than 128 bytesâ Dalvenjia
Mar 15 at 21:06
other option is to count the lines,
for F in *.xml; do if [ $(wc -l "$F") -lt 4 ]; then echo "$F"; fi; done
â Dalvenjia
Mar 15 at 21:11
1
@Dalvenjia: very bad idea. Think of an inline node like
<foo>xxxxxxxxxxxxxxxxxxxxxxxx</foo>
â Gilles Quenot
Mar 15 at 21:44
What about comments and processing instructions?
â ErikF
Mar 15 at 23:07