I'm trying to figure out how to open an xml file, search by an id, replace a value in the node and then resave the document.
my xml
<?xml version="1.0"?>
<data>
<user id="1370018670618">
<email>1#1.com</email>
<sent>false</sent>
</user>
<user id="1370018701357">
<email>2#2.com</email>
<sent>false</sent>
</user>
<user id="1370018769724">
<email>3#3.com</email>
<sent>false</sent>
</user>
<user id="1370028546850">
<email>4#4.com</email>
<sent>false</sent>
</user>
<user id="1370028588345">
<email>5#5.com</email>
<sent>false</sent>
</user>
</data>
My code to open and find a node
xml_content = File.read("/home/mike/app/users.xml")
doc = Nokogiri::XML(xml_content)
node_update = doc.search("//user[#id='1370028588345'] //sent")
node_update.inner_html ##returns value of "sent"
the part in this where I'm stuck is actually updating the node. node_update.inner_html = "true" returns a method error on inner_html. then after that saving the updated file.
First of all, your node_update is actually a NodeSet, not the Node that you probably think it is. You need a Node if you want to call inner_html= on it:
node_update[0].inner_html = 'true'
Then writing out the updated XML is just a bit of standard file manipulating combined with a to_xml call:
File.open('whatever.xml', 'w') { |f| f.print(doc.to_xml) }
As an aside, your input isn't valid XML. You have a </details> but no <details>.
Related
It seems that Nosqlunit-neo4j is not compatible with SDN 4 since TypeRepresentationStrategy is removed. It adds the node defined in following graphml xml file into test database but doesn't assign it a label due to which repository.count() returns 0. However, if I query the database natively, then it does fetches the node without any Label.
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns">
<key id="__type__" for="node" attr.name="__type__" attr.type="string"></key>
<key id="productId" for="node" attr.name="productId" attr.type="string"></key>
<graph id="G" edgedefault="directed">
<node id="3">
<data key="__type__">com.my.package.Product</data>
<data key="productId">100001235</data>
<index name="__types__" key="className">com.my.package.Product
</index>
</node>
</graph>
</graphml>
Does anyone facing the same issue?
If you use the label Product directly it should work.
You don't need the index or the __type__ properties anymore.
I was to create XML as below and save it as a string. The values for the XML will be from sqlite database. I want to pass the string value which will be in a xml format through a WebService, so for that reason I want to create XML and save it as a string. Please help me with this.
1st XML:
<?xml version="1.0"?>
<EmployeeHeader xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<EmplID>1001</EmplID>
<EmplName>Jack</EmplName>
<Designation>Manager</Designation>
<Department>Sales</Department>
</EmployeeHeader>
2nd XML:
<?xml version="1.0"?>
<EmployeeDetail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Address>
<City>Philadelphia</City>
<State>PA</State>
<Country>USA</Country>
</Address>
</EmployeeDetail>
It's not completely clear what you're asking, but if I'm understanding you correctly, you just want to build an XML string substituting the values you get from a database?
Something like:
NSString *xml = [NSString stringWithFormat:#"<EmployeeDetail><Address><City>%#</City><State>%#</State><Country>%#</Country></Address>", addr.city, addr.state, addr.country];
I have an xml type grails.converters.deep.XML
<?xml version="1.0" encoding="UTF-8"?>
<list>
<customer>
<name>A</name>
<age>1</age>
</customer>
<customer>
<name>B</name>
<age>2</age>
</customer>
<customer>
<name>C</name>
<age>3</age>
</customer>
</list>
How do I get each customers and how do I convert it to customer domain object?
Thanks,
Nimmy
I think the deep converters have been deprecated. The new way is supposed to be something like:
XML.use( 'deep' ){ ... }
http://johnrellis.blogspot.co.uk/2009/11/grails-and-json-are-pretty-groovy.html
But you should really be using the new render as json functionality in grails 2
http://grails.org/doc/latest/guide/theWebLayer.html#xmlAndJSON
I have noticed that parseKml() function does not parse KML file correctly. For example, if you have this bit of KML:
<ExtendedData>
<Data name="Offer">
<value>Apples</value>
<value>Potatoes</value>
<value>Tomatoes</value>
</Data>
</ExtendedData>
The parseKml() function will return a kmlObject that will contain only the last value, i.e. "Tomatoes":
Does anyone have a solution for this?
The structure for the type of extended data you are using is for name/value pairs. i.e. a single name with a single value.
<ExtendedData>
<Data name="string">
<displayName>...</displayName> <!-- string -->
<value>...</value> <!-- string -->
</Data>
</ExtendedData>
So what you are trying will not work. If you wish to add an arbitrary XML Data structure to a KML Feature then you would do it like this.
<ExtendedData xmlns:offer="http://yourserver.com/namespace">
<offer:item>Apples</offer:item>
<offer:item>Potatoes</offer:item>
<offer:item>Tomatoes</offer:item>
</ExtendedData>
Based on the data structure, the 'offer' XML Schema file (http://yourserver.com/namespace) would be something like.
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="item" type="xsd:string" />
</xsd:schema>
I believe the <Data> element can only contain a single <value> - according to the docs, the "<Data> element allows you to add untyped name/value pairs to the user data associated with a given Feature."
So in your case, it's picking up the last <value> element only. You might find another way to add your custom data here: https://developers.google.com/kml/documentation/extendeddata
I have the below xml's in my code
XML Parsing Error: not well-formed
Location: http://localhost:3000/api/client?client=test1
Line Number 1, Column 1111:
<?xml version="1.0" encoding="UTF-8"?>
<application>
<name><![CDATA[TESTapp2]]></name>
<application-identifier>wac-8c28afa4-0f6e-11e1-8885-7071bc62c7bc</application-identifier>
<clients>
<pricepoint id="1" name=<![CDATA[TEST-price]]> currency="dollar" locale="la" country="india" price="50" text="this is a TEST" receipt="oi120934" operator-reference="1213w" operator-id="1"></pricepoint></pricepoints><product-image></product-image>
</clients>
</application>
<name><![CDATA[TESTapp2]]></name> this is working
<name=\"[CDATA[TESTapp2]]\"> this is not working,throws encoding error
AFAIK, Using CDATA as an attribute value is forbidden. CDATA can only be used for text nodes.