Hi guys,
I wanted to know how to set node value using xpath, for example
<root>
<name></name>
</root>
what I want is get the node 'name' and set its value to say 'bob', so it will be like
<name>bob</name>
and thus save this data in xml file.
Thanks in advance
7 18379
x-path itself is not meant for programming. Are you using .NET?
it could like so:
document.selectSingleNode("name").Value = "bob";
x-path itself is not meant for programming. Are you using .NET?
it could like so:
document.selectSingleNode("name").Value = "bob";
Sorry, I forgot to mention the programming language I'm using java and tried something like this - methodXPath(){
-
XPathFactory factory = XPathFactory.newInstance();
-
XPath xPath = factory.newXPath();
-
XPathExpression expression = xPath.compile("expression");
-
NodeList results = (NodeList) expression.evaluate(new InputSource("data.xml"), XPathConstants.NODESET);
-
traverse(results);
-
}
-
-
-
traverse(NodeList rootNode){
-
for(int index = 0; index < rootNode.getLength();
-
index ++){
-
Node aNode = rootNode.item(index);
-
if (aNode.getNodeType() == Node.ELEMENT_NODE){
-
NodeList childNodes = aNode.getChildNodes();
-
if(aNode.getNodeName().equalsIgnoreCase("address")){
-
aNode.setTextContent("some address");
-
}
-
}
-
traverse(aNode.getChildNodes());
-
}
-
}
When I run the program, at runtime the 'address' element shows the value 'some address' in console, but the value is not written to the xml file.
I was wondering if XPath is read-only..?
How can I make this value get stored in xml file.
new InputSource("data.xml"),
You're only specifying data.xml as an inputSource.
If you want to actually modify this file, I suggest using a document object,
eg, in this case JDOM, then Element methods, then XMLOutputter
http://forum.java.sun.com/thread.jspa?threadID=631744&messageID=3647587
http://www.jdom.org/docs/apidocs/org/jdom/output/XMLOutputter.html
new InputSource("data.xml"),
You're only specifying data.xml as an inputSource.
If you want to actually modify this file, I suggest using a document object,
eg, in this case JDOM, then Element methods, then XMLOutputter
http://forum.java.sun.com/thread.jspa?threadID=631744&messageID=3647587
http://www.jdom.org/docs/apidocs/org/jdom/output/XMLOutputter.html
Hi,
Thanks a lot for ur help, I fixed the problem :)
HI messagewalker
Can you kidnly let me know how you fixed it..I'm also trying the same thing and its not wriitng to xml file
Rgds
Ramraj
HI I 'm looking for the same code
need to update content node ,using JAva or C# xpath
could you please show the codes
As stated before, it depends on what you're using to write the node. See post #4 for the link to use XMLOutputter.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: bdinmstig |
last post by:
I refined my attempt a little further, and the following code does
seem to work, however it has 2 major problems:
1. Very limited support for XPath features
Basic paths are supported for...
|
by: Murtaza Tinwala |
last post by:
Hi mates,
I have the following problem in XSLT. I have following variables:
ref-file = <path of XML document> eg "xmlDoc.xml"
repeatpath = <a repeat path expressed in Xpath like /root/person >...
|
by: Felicity |
last post by:
Hello,
The following XML file shows an example of some config
data for a Side Button Bar control. There are two forms
this Button Bar control relates to, "frmMainMenu"
and "Listing".
<?xml...
|
by: orit |
last post by:
I have the following xml file:
<?xml version="1.0" encoding="utf-8" ?>
<course id="2555" title="Developing Microsoft .NET Applications for Windows
(Visual C# .NET)" length="5 days"...
|
by: Ken Getz |
last post by:
Hi. I have an xml file in this format:
<strings>
<string>Item1</string>
<string>Item2</string>
<string>Item3</string>
<string>Item4</string>
</string>
I'm looking for the best way to...
|
by: Mike |
last post by:
Hi,
I'm a complete newbie to XML and am needing to rush through a job requiring
far more knowledge than I have the time to gather.
Basically... I need to build a breadcrumb menu for our...
|
by: nick_tucker |
last post by:
Hi,
I am very new to XML and XPATH. I have made a sample XML fileto ask
my question.
<?xml version="1.0" encoding="utf-8"?>
<Test>
<A>
<B>
<C>
<D>
|
by: luthriaajay |
last post by:
I need some help to extract the LatestFillQuantity element value using XPATH.
in Java.
I am unable to extract the value of 10000.
Please help as to what have I done wrong.?
Help appreciated.
...
|
by: =?Utf-8?B?WVhR?= |
last post by:
I want to do the multi-language program, save the language text in XML file,
but how to read the specified node value? the xml is below, for example, i
want to get the value(AAA content) that named...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |