- It requires xmllint command. libxml2-utils need to be installed:
sudo apt install libxml2-utils
- Use vim open the xml file, use the following vim command to reformat:
:%!xmllint --format -
Search This Blog
Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts
vim: format/indent xml file
processing XML using powershell
# load XML document from file
$xmlfile = "C:\Users\user1\Documents\123.xml"
[xml]$doc = Get-Content $xmlfile
# find element by tag name
$te = $doc.DocumentElement.SelectSingleNode("token")
# check if element exists
if (-not $te) {
# create the element if not exist
$te = $doc.CreateElement("token")
# append to DocumentELement
$doc.DocumentElement.AppendChild($te)
}
# set element value
$te.InnerText = "XXXXXXXX"
# save the document back to the file
$doc.Save($xmlfile)
see also
Bash: replace substring using regular expressions
With bash, you can replace string using ${variable//pattern/replacement}
a="<token>XXX</token>"
# to replace the token element value with YYY
a=${a//<token>[a-zA-Z0-9]*<\/token>/<token>[a-zA-Z0-9]*<\/token>}
see also
Java: Remove illegal character 0x0 from string
Unicode 0x0 is illegal in XML.
str.replaceAll("[\\000]+", "")
Python: ElementTree XML element to string
import xml.etree.ElementTree as ET
# element is an xml.etree.ElementTree.Element object
xmlstr = ET.tostring(element, encoding='utf8', method='xml')
See also
Python: parse xml using ElementTree
- parse from a file:
import xml.etree.ElementTree as ET
tree = ET.parse('/home/wilson/my.xml')
root = tree.getroot() - parse from a string:
import xml.etree.ElementTree as ET
root = ET.fromstring('1')
see also
Python: get the sub-elements of a xml element
According to Python XML ElementTree API,
- get all sub-elements(descendants):
all_descendants = list(element.iter())
- get direct sub-elements(children):
children = list(element)
XML element naming rules
XML elements must follow these naming rules:
- Names can contain letters, numbers, and other characters. Except for the first character, they can contain
- digits
- period(.)
- hyphen(-)
- underscore(_)
- colon(:) - legal but should be used except for namespaces
- NO other characters are allowed like #, @, $, %
- Names cannot start with a number or punctuation character. Must start with a letter or a underscore(_).
- Names cannot start with the letters xml (or XML, or Xml, or xMl etc)
- Names cannot contain spaces
Javascript: show XML object as string
IE:
alert(XmlObj.xml);
Firefox:
alert((new XMLSerializer()).serializeToString(XmlObj));
alert(XmlObj.xml);
Firefox:
alert((new XMLSerializer()).serializeToString(XmlObj));
Subscribe to:
Posts (Atom)