---
title: "Hämta attribut från xml"
type: "forum-thread"
url: "https://www.webforum.nu/amne/asp/191695-hämta-attribut-från-xml"
topic: "ASP"
topic_url: "https://www.webforum.nu/amne/asp"
author: "fedde"
published: "2013-08-27T10:11:56.000Z"
updated: "2013-08-28T00:09:56.000Z"
replies: 2
views: 1756
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/asp/191695-hämta-attribut-från-xml"
---

# Hämta attribut från xml

## #1 — fedde, 2013-08-27T10:11Z

Jag behöver plocka ut consignmentId och orderNo från nedan XML. Men jag får inte till det.

XML:

```
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
	<S:Body>
		<ns4:bookResponse xmlns:ns2="http://www.x.com/consignment/types/v1_0" xmlns:ns3="http://www.x.com/common/webservice/types/v1_0" xmlns:ns4="http://www.x.com/consignment/types">
			<result>
				<consignments consignmentId="137600459735850de1903" orderNo="fe990255">
```

Det börjar ju med

```
Dim xmlDoc, fil
set xmlDoc=Server.CreateObject("Msxml2.DOMDocument.4.0")
xmlDoc.async="false"
fil=Server.mappath("testa.xml")
xmlDoc.load(fil)

Dim root
Set root = xmlDoc.documentElement
```

Sedan har jag provat flera sätt men inget funkar för mig. Bl.a:

```
Set objNamedNodeMap = xmlDoc.documentElement.childNodes(4)
intConsignmentId = objNamedNodeMap.attributes.getNamedItem("consignmentId").text
response.write intConsignmentId
```

Förslag?

Permalänk: https://www.webforum.nu/p/191695

## #2 — voigtann1, 2013-08-27T16:51Z

Du kan använda dig av XPath kanske, kan tycka det är lite enklare att jobba med:

```
xmlDoc.setProperty "SelectionLanguage", "XPath"
```

och använd 

```
xmlDoc.selectSingleNode("//result/consignments/@consignmentId")
```

så borde du få ut consignements sen om det är text eller attribute du vill använda dig av så få du nog ändra koden lite.

Permalänk: https://www.webforum.nu/p/2313895

## #3 — fedde, 2013-08-28T00:09Z

Tackar!

Nu fick jag ihop två fungerande varianter :-)

```
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
'xmlDoc.load(Server.MapPath("svar.xml")) 'Om XML i fil
xmlDoc.loadXML(xmlhttp.responseText) 'Om XMLretur via t.ex. SOAP
xmlDoc.setProperty "SelectionLanguage", "XPath"

Dim strConsignmentId, strOrderNo
strConsignmentId = xmlDoc.selectSingleNode("//consignments/@consignmentId").text
strOrderNo = xmlDoc.selectSingleNode("//consignments/@orderNo").text

response.write "<br>strConsignmentId: " & strConsignmentId & VbCrLf
response.write "<br>strOrderNo: " & strOrderNo & VbCrLf

Set xmlDoc = Nothing
```

```
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
'xmlDoc.load(Server.MapPath("svar.xml")) 'Om XML i fil
xmlDoc.loadXML(xmlhttp.responseText) 'Om XMLretur via t.ex. SOAP
xmlDoc.setProperty "SelectionLanguage", "XPath"

Dim data, strConsignmentId, strOrderNo
set data = xmlDoc.selectSingleNode("//result/consignments")
strConsignmentId = data.attributes.getNamedItem("consignmentId").text
strOrderNo = data.attributes.getNamedItem("orderNo").text

response.write "<br>strConsignmentId: " & strConsignmentId & VbCrLf
response.write "<br>strOrderNo: " & strOrderNo & VbCrLf

Set data = Nothing
Set xmlDoc = Nothing
```

Tack så rackarns mycket! :bire

Permalänk: https://www.webforum.nu/p/2313898

---

Tråden på webben: https://www.webforum.nu/amne/asp/191695-hämta-attribut-från-xml
