How to Read Xml File in Linux

how to read xml file in linux

You can use this sed,

sed -n '/LANGUAGE/{N; s/.*<string>\(.*\)<\/string>.*/\1/p; }' Locale.xml

How to parse XML using shellscript?

You could try xmllint

The xmllint program parses one or more
XML files, specified on the command
line as xmlfile. It prints various
types of output, depending upon the
options selected. It is useful for
detecting errors both in XML code and
in the XML parser itse

It allows you select elements in the XML doc by xpath, using the --pattern option.

On Mac OS X (Yosemite), it is installed by default.

On Ubuntu, if it is not already installed, you can run apt-get install libxml2-utils

Linux xml parse

The stuff before a colon is a name space prefix. You need to register the namespace and use it in the XPath expressions:

setns s=urn:S2SCTIcf:xsd:$SCTIcfBlkCredTrf
cat //s:FDtTm/text()

So, the whole script can be something like

#!/bin/bash

ext=$(cat <<'EOF' \
| xmllint --shell file.xml \
| grep -A1 -- '-------' \
| tail -n1
setns s=urn:S2SCTIcf:xsd:$SCTIcfBlkCredTrf
cat //s:FileRef/text()
EOF
)
echo "$ext"

How to read XML and write to text file using shell script?

Try following and let me know if this helps you.

awk '
/<\/Data>/{
a="";
next
}
/<Data>/{
a=1;
next
}
/location/ && a{
gsub(/\"|>/,"",$NF);
location=$NF;
next
}
/Name/ && a{
name=$2;
next
}
/ID/ && a{
print "Name : ",name RS "ID: ",$2 RS "Location: ",location;
next
}
' Input_file

As per OP's request sine NO TAGS should be hard coded so adding following solution too now.

EDIT2: I am not a xml expert but tried it here, could you please check once.

awk 'NF==1 && (/ +<[a-zA-Z]+>/ || /^<[a-zA-Z]+>/ || / +<\/[a-zA-Z]+>/){
next
}
{
sub(/^ +/,"")
gsub(/\"|<|>/,"",$0);
sub(/\/.*/,"");
if($0){
print
}
}
' Input_file

How linux daemon will read xml file in worker service .Net Core 3.1

You are right that a Linux daemon will not be able to read paths that dont exist on Linux.

There are a couple of approaches you can look at:

  1. Your services can look at the OS and read a different path/file on Linux. For example, for Windows you are reading the hard-coded location @'D:\LC2\Files\Config.xml', and on Linux, you can use another hard-coded like /opt/LC2/config.xml.

  2. You can provide an argument to the worker service. That requires parsing arguments and teaching your worker service about an argument, like --config /path/to/file. Then your worker service will just read that passed-in path and not have to worry about the path. Whatever runs/configures your service will provide a correct path on Windows and on Linux.

  3. You can add support for reading the file location via an environment variable like LC2_CONFIG. If it's set and points to a file, use that location as the config file. Otherwise fall back to the hard-coded value. On Linux, the tool that runs your daemon can provide it a path that is appropriate for Linux.

There are other options and variants possible, but these seem like the most obvious approaches.

Depending on the audience of the program, you might want all three. For example, git will use a default location for a config file. But it will let users provide specific configuration values both via command line and also let users specify configuration via environment variables.

how to parse xml file in C language on linux

Considering you might not need libXML just to extract a few value, and as long as you don't care about the validity of your input, you could load the file in memory ( or part of it ).

char* xmlChunk; // your data will be stored inside
char* tagStartBegin = strstr(xmlChunk,"<pty");
char* tagStartEnd = strstr(tagStartBegin,">");
char* value = tagStartEnd+1;
// get the end tag
char* tagEndBegin = strstr(tagStartEnd,"</pty>");
//end the string here
*tagEndBegin = '\0';
// parse your value
doSomeThing(value);

You might need to adapt your code for unicode input if any



Related Topics



Leave a reply



Submit