← 返回命令列表

Linux command

xq 命令

文件

复制后可按需替换文件名、目录或参数。

常用示例

Convert XML to JSON

xq . [file.xml]

Extract value using jq syntax

xq '.[key]' [file.xml]

Convert and format XML

cat [file.xml] | xq .

Convert JSON back to XML

xq -x . [file.json]

Process multiple XML files

xq . [a.xml] [b.xml]

Stream large XML document

xq . --xml-item-depth [2] [large.xml]

Wrap output in XML root element

xq -x --xml-root [root] . [file.json]

说明

xq is a command-line XML processor that wraps jq. It transcodes XML documents to JSON using xmltodict, pipes them through jq for processing, and optionally converts the result back to XML. The tool enables using jq's powerful JSON query syntax on XML data. All standard jq filters and operations work on the converted JSON representation. For large XML documents, the --xml-item-depth option enables streaming mode, processing elements at a specified depth without loading the entire document into memory. Entity expansion and DTD resolution are disabled by default for security. Install via: `pip install yq` or `brew install python-yq`

参数

-x, --xml-output
Transcode output back to XML.
--xml-item-depth _n_
Stream items at depth n (for large documents).
--xml-dtd
Preserve Document Type Definition.
--xml-root _name_
Wrap XML output in element with this name.
--xml-force-list _element_
Force element to always be a list.
-i, --in-place
Edit files in place.
-c, --compact-output
Compact JSON output.
-r, --raw-output
Raw string output.
--version
Display version.
-h, --help
Display help.

FAQ

What is the xq command used for?

xq is a command-line XML processor that wraps jq. It transcodes XML documents to JSON using xmltodict, pipes them through jq for processing, and optionally converts the result back to XML. The tool enables using jq's powerful JSON query syntax on XML data. All standard jq filters and operations work on the converted JSON representation. For large XML documents, the --xml-item-depth option enables streaming mode, processing elements at a specified depth without loading the entire document into memory. Entity expansion and DTD resolution are disabled by default for security. Install via: `pip install yq` or `brew install python-yq`

How do I run a basic xq example?

Run `xq . [file.xml]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -x, --xml-output do in xq?

Transcode output back to XML.