Hi I have an xml file like below,
<Pods>
<item>
<URL>data/_data/2014/09/11/pods/10057-1837887-2965978-0.pdf</URL>
<RunDate>11/09/2014</RunDate>
<DateSigned>11/09/2014 09:13:49
</DateSigned>
</item>
<item>
<URL>data/_data/2014/09/11/pods/10057-0-2965978-0-scan.pdf</URL>
<DateSigned>Not signed</DateSigned>
</item>
</Pods>
I would like to get the conents of the <URL> where <DateSigned> is not equal to "Not Signed"
I have tried
Dim URLNode As XmlNodeList = doc.SelectNodes("//ITEM[DateSigned=Not Signed]/URL")
but this says invalid token I am unsure what I have dine wrong.
Thanks for any help
You need to simply wrap Not signed within single-quotes to make it recognized as string value. Also, XML is case-sensitive, use the correct cases :
Dim URLNode As XmlNodeList = _
doc.SelectNodes("//item[DateSigned='Not signed']/URL")
or if you really meant to get <item> where <DateSigned> is not equals Not signed :
Dim URLNode As XmlNodeList = _
doc.SelectNodes("//item[DateSigned != 'Not signed']/URL")
Related
I'm making an XML API call in Postman and I get the following response:
<response>
<result>
<system>YYY:XXXXXXXXX</system>
</result>
</response>
I created a variable from the content in "system", but I need to remove the "YYY:" from the front. Here is how I created the variable:
var response = xml2Json(responseBody);
var uuid = response["response"]["result"]["system"];
pm.collectionVariables.set("uuid", uuid);
If that could be formatted to remove any possible other characters before the colon - ex: "CCC:" or "ABC:" or "VAR1:" - that would be a bonus, but hard-coding this to remove exactly "YYY:" would solve the current problem.
You could do this, split String by character ":"
let text = "YYY:XXXXXXXXX";
console.log(text.split(":")[1]);
Apply to your code:
var response = xml2Json(responseBody);
var uuid = response["response"]["result"]["system"];
pm.collectionVariables.set("uuid", uuid.split(":")[1]);
Result:
I know that there are many questions to this topic.. But I can't answer my question with that..
Can someone help me how to do that?
My xml file looks like that:
...
<object>
<acronym>Mmachc</acronym>
<alias-tags>1810037K07Rik RP23-177C18.3</alias-tags>
<chromosome-id>49</chromosome-id>
<ensembl-id nil="true"/>
<entrez-id>67096</entrez-id>
<genomic-reference-update-id>491928275</genomic-reference-update-id>
<homologene-id>12082</homologene-id>
<id>42939</id>
<legacy-ensembl-gene-id nil="true"/>
<name>
methylmalonic aciduria cblC type, with homocystinuria
</name>
<organism-id>2</organism-id>
<original-name>
</original-name>
<original-symbol>Mmachc</original-symbol>
<reference-genome-id nil="true"/>
<sphinx-id>95240</sphinx-id>
<version-status>no change</version-status>
</object>
<object>
...
So if I now want to search the object that contains e.g. the entrez-id 67096 to see which acronym it has.. I tried first:
url = "http://api.brain-map.org/api/v2/data/query.xml?num_rows=10000&start_row=10001&&criteria=model::Gene,rma::criteria,products[abbreviation$eq%27Mouse%27]"
req = requests.get(url)
doc = req.text
root = etree.XML(doc)
soup = BeautifulSoup(doc)
dict1 = {}
for object in soup.find_all('object'):
dict1[object.find('entrez-id') == 67096]
The output for that is KeyError: False..
Can someone help me with that?
Also if I try to find it as string '67096' I got key error false..
You don't really need beautifulsoup here; just try something like:
target = root.xpath('//entrez-id[.="67096"]/preceding-sibling::acronym/text()')
target[0]
Output:
'Mmachc'
Using response As WebResponse = request.GetResponse()
Using rd As New StreamReader(response.GetResponseStream())
Dim soapResult As String = rd.ReadToEnd()
SerializeCollection(soapResult)
the soapResult generate the following :
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<createShipmentResponse xmlns="http://www.royalmailgroup.com/api/ship/V2">
<integrationHeader>
<dateTime xmlns="http://www.royalmailgroup.com/integration/core/V1">2016-03-23T06:55:32</dateTime>
<version xmlns="http://www.royalmailgroup.com/integration/core/V1">2</version>
<identification xmlns="http://www.royalmailgroup.com/integration/core/V1">
<applicationId>RMG-API-G-01</applicationId>
<transactionId>730222611</transactionId>
</identification>
</integrationHeader>
<integrationFooter>
<errors xmlns="http://www.royalmailgroup.com/integration/core/V1">
<error>
<errorCode>E1105</errorCode>
<errorDescription>The countryCode specified is not valid</errorDescription>
</error>
<error>
<errorCode>E1001</errorCode>
<errorDescription>Postcode AA9 0AA invalid</errorDescription>
</error>
</errors>
<warnings xmlns="http://www.royalmailgroup.com/integration/core/V1">
<warning>
<warningCode>W0036</warningCode>
<warningDescription>E-mail option not selected so e-mail address will be ignored</warningDescription>
</warning>
<warning>
<warningCode>W0035</warningCode>
<warningDescription>SMS option not selected so Telephone Number will be ignored</warningDescription>
</warning>
</warnings>
</integrationFooter>
</createShipmentResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
i am using this method to serialize this response :
Private Sub SerializeCollection(filename As String)
Dim Emps As createShipmentResponse = New createShipmentResponse()
' Note that only the collection is serialized -- not the
' CollectionName or any other public property of the class.
Dim x As XmlSerializer = New XmlSerializer(GetType(createShipmentResponse))
Dim writer As TextWriter = New StreamWriter(filename)
x.Serialize(writer, Emps)
writer.Close()
End Sub
but i am having this error : Illegal characters in path
what does that mean ? and how can i fix this ?
Your SerializeCollection() method expects a file path to serialize to a file. You're currently passing the contents of your response which is why it doesn't work.
If you haven't written the method yourself I think you've not found completely what you're looking for.
This is an example of how the method should be called:
SerializeCollection("C:\Users\Vincent\Desktop\Hello.bin") 'The extension doesn't need to be '.bin', but it's an example.
The method has currently no way of getting your response, for that you should add a second parameter.
As I'm not familiar with Soap serialization I'm afraid I cannot help you further.
I am trying to read this XML document.
An excerpt:
<datafile xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="wiitdb.xsd">
<WiiTDB version="20100217113738" games="2368"/>
<game name="Help Wanted: 50 Wacky Jobs (DEMO) (USA) (EN)">
<id>DHKE18</id>
<type/>
<region>NTSC-U</region>
<languages>EN</languages>
<locale lang="EN">
<title>Help Wanted: 50 Wacky Jobs (DEMO)</title>
<synopsis/>
</locale>
<developer>HUDSON SOFT CO., LTD.</developer>
<publisher>Hudson Entertainment, Inc.</publisher>
<date year="2009" month="" day=""/>
<genre>party</genre>
<rating type="ESRB" value="E10+">
<descriptor>comic mischief</descriptor>
<descriptor>mild cartoon violence</descriptor>
<descriptor>mild suggestive themes</descriptor>
</rating>
<wi-fi players="0"/>
<input players="2">
<control type="wiimote" required="true"/>
<control type="nunchuk" required="true"/>
</input>
<rom version="" name="Help Wanted: 50 Wacky Jobs (DEMO) (USA) (EN).iso" size="4699979776"/>
</game>
So far I have this:
Dim doc as XPathDocument
Dim nav as XPathNavigator
Dim iter as XPathNodeIterator
Dim lstNav As XPathNavigator
Dim iterNews As XPathNodeIterator
doc = New XPathDocument("wiitdb.xml")
nav = doc.CreateNavigator
iter = nav.Select("/WiiTDB/game") 'Your node name goes here
'Loop through the records in that node
While iter.MoveNext
'Get the data we need from the node
lstNav = iter.Current
iterNews = lstNav.SelectDescendants(XPathNodeType.Element, False)
'Loop through the child nodes
txtOutput.Text = txtOutput.Text & vbNewLine & iterNews.Current.Name & ": " & iterNews.Current.Value
End While
It just skips the "While iter.MoveNext" part of the code. I tries it with a simple XML file, and it works fine.
I think your XPath query is off. WiiTDB is a closed node, so you need to look for /datafile/game or //game.
Use the System.Xml.Serialization namespace instead: create a dedicated, serializable class to hold the data you wish to load and define shared serialize / deserialize functions with strongly typed arguments to do the work for you.
As the structure of the new classes will closely follow that of your XML data, there should be no confusion as to which data is located where within a run time instance.
See my answer here for an idea of how to create a class from an example XML file.
I've been using XML serialization for a while, and today I realized something really odd. If I have a new line right after a "dot" (.), when i deserialize, I lose the dot. Has anyone ever had this happen to them? The following is my serialization code:
Serialize
Dim xmlSerializer As New System.Xml.Serialization.XmlSerializer(GetType(SilverWare.Licensing.Common.StoreLicense), New System.Type() {GetType(SilverWare.Licensing.Common.StationLicense)})
Dim gen As LicenseGenerator
If store Is Nothing Then
Throw New ArgumentNullException("store")
ElseIf store.StationLicenses Is Nothing Then
Throw New ArgumentNullException("store.StationLicenses")
ElseIf store.StationLicenses.Length = 0 Then
Throw New ArgumentOutOfRangeException("store.StationLicenses", "Must contain at least one element.")
End If
' Create a license generator for issuing new license keys.
gen = New LicenseGenerator(store)
' Generate store key.
store.LicenseKey = gen.GenerateLicenseKey
' Generate individual station keys.
For Each station In store.StationLicenses
station.LicenseKey = gen.GenerateLicenseKey(station)
Next
' Write license to file.
Using xFile As Xml.XmlWriter = Xml.XmlWriter.Create(licenseFile)
xmlSerializer.Serialize(xFile, store)
xFile.Close()
End Using
Deserialize
Dim xmlDeserializer As New System.Xml.Serialization.XmlSerializer(GetType(SilverWare.Licensing.Common.StoreLicense), New System.Type() {GetType(SilverWare.Licensing.Common.StationLicense)})
Dim result As SilverWare.Licensing.Common.StoreLicense
Using xFile As Xml.XmlReader = Xml.XmlReader.Create(licenseFile)
result = DirectCast(xmlDeserializer.Deserialize(xFile), SilverWare.Licensing.Common.StoreLicense)
xFile.Close()
End Using
Return result
The really funny part is that if I have a space after the dot, or remove the new line character, there are no problems. This only happens if it is dot which I find mind boggling.
Here is a quick sample of my XML file that was created when I serialized:
<?xml version="1.0" encoding="utf-8" ?>
<StoreLicense xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
...
<ReceiptAddress>98 N. Washington St.
Berkeley Springs West Virginia</ReceiptAddress>
<Name>Ambrae House at Berkeley Springs</Name>
<AliasName>Ambrae House</AliasName>
<Address1>98 N. Washington St.</Address1>
<Address2 />
...
</StoreLicense>
The line that is having the problem is the ReceiptAddress Node.
This post on MSDN seems to answer your question.
MSDN: Serialize String containing only whitespace such as a " " character
From that post, try this:
<XmlAttribute("xml:space")> _
Public SpacePreserve As [String] = "preserve"
This creates a root node like the following:
<DataImportBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:space="preserve">
Jim
Since I was using someone elses dll, I didn't even think that it would be modifying my data when we imported it. What was happening was that the other programmer had a reg_ex that was looking for a dot before a new line. That was my issue, and my grief for 3 months.