Getting an Attribute Value in Xml Element

Getting an attribute value in xml element

I think I got it. I have to use org.w3c.dom.Element explicitly. I had a different Element field too.

Getting attribute value of an XML Document using C#

I would try something like this:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<reply success=\"true\">More nodes go here</reply>");

XmlElement root = doc.DocumentElement;

string s = root.Attributes["success"].Value;

Get a xml element with specific attribute value in c#

xdoc.Elements() returns only one element - the Root of XML tree (it's <Help> element in your example.

Change your query to:

IEnumerable<XElement> list =
(from el in xdoc.Root.Elements()
where (string)el.Attribute("Name") == "creatingTests"
select el);

It returns collection with one element. Use First or FirstOrDefault to get it as single item, not a collection:

XElement item = (from el in xdoc.Root.Elements()
where (string)el.Attribute("Name") == "creatingTests"
select el).FirstOrDefault();

AWK get attribute value from XML element

assuming no newlines within tag

gawk/mawk/mawk2 'BEGIN { FS = "version=\"" } /^[<]pkg-info/ {

print substr($2, 1, index($2, "\"") -1 ); exit; }'

version to handle random \n

gawk/mawk/mawk2 'BEGIN { FS="version=\"" } (NF > 1) { 

if (seen++) { print substr($2,1,index($2, "\"")-1); exit; } }'

This will skip the first time it sees version, at the initial xml tag. second time it prints the version number then exits. this code does not need to make assumptions regarding how version numbers are formatted, other than being double quoted.

version to account for pkg-info being all over the place :

gawk/mawk/mawk2 'BEGIN { RS = "^$"; FS = "([<]pkg-info|[\/]pkg-info[>])";

} match($2, /version=[^ ]+/) {

print substr($2, RSTART + 9, RLENGTH - 10); exit; }'

Just have it read in the whole XML file, not attempting to split things along NL. Then when you enforce FS exactly being the opening and close tags of it, then $2 must be the first occurrence of such a tag.

How to get attribute value and element value from XML with xmllint

Another option would be to use xmlstarlet to match the model elements and then use concat() to output the desired values...

xmlstarlet sel -t -m "//_:model" -v "concat(@mh,' ',_:attribute)" -n devices.xml

outputs...

0x1058905 prod-vpn-gw-v01.e-x.com
0x1058907 prod-storage-san-z01-ssh.e-x.com
0x1058900 test-vpn-gw-v01

Note: I'm using version 1.6.1 of xmlstarlet. Not all versions support "_" for a namespace prefix. (Supported in versions 1.5.0+)

See http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html#idm47077139652416 for more info on the "sel" command of xmlstarlet.

Need to get the attribute value from the another XML file element

I would use a key and simply match the attributes class having a reference and transform them

  <xsl:param name="class-map">
<name>
<old>heading</old>
<new>Headings</new>
</name>
<name>
<old>normal</old>
<new>Actual</new>
</name>
</xsl:param>

<xsl:key name="class-map" match="name/new" use="../old"/>

<xsl:template match="p/@class[key('class-map', tokenize(.), $class-map)]">
<xsl:attribute name="{name()}" select="key('class-map', tokenize(.) , $class-map)"/>
</xsl:template>

Handle the rest by the identity transformation. https://xsltfiddle.liberty-development.net/93dFK9P has an XSLT 3 online sample. Of course the document class-map could be loaded with doc or document (e.g. <xsl:param name="class-map" select="doc('class.xml')"/>) from a file or a URI in general, only inline above for completeness and compactness of the example.

C# - Get value of xml element with its attribute

The Elements query only finds immediate children of the current node. i.e. Elements will only return the root list element.

This either means you need to query like:

doc.Elements("list").Elements("resources").Elements(...

Or you can use Descendants, which will many any descendant of the current node, so:

var name = (string)doc.Descendants("field")
.First(x => (string)x.Attribute("name") == "name");

How to get the attribute value of an xml node using java

Since your question is more generic so try to implement it with XML Parsers available in Java .If you need it in specific to parsers, update your code here what you have tried yet

<?xml version="1.0" encoding="UTF-8"?>
<ep>
<source type="xml">TEST</source>
<source type="text"></source>
</ep>
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("uri to xmlfile");
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//ep/source[@type]");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

for (int i = 0; i < nl.getLength(); i++)
{
Node currentItem = nl.item(i);
String key = currentItem.getAttributes().getNamedItem("type").getNodeValue();
System.out.println(key);
}


Related Topics



Leave a reply



Submit