473,324 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

how to set node value using xpath

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
Aug 13 '07 #1
7 18379
jkmyoung
2,057 Expert 2GB
x-path itself is not meant for programming. Are you using .NET?

it could like so:
document.selectSingleNode("name").Value = "bob";
Aug 13 '07 #2
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

Expand|Select|Wrap|Line Numbers
  1. methodXPath(){
  2. XPathFactory factory = XPathFactory.newInstance();
  3. XPath xPath = factory.newXPath();
  4. XPathExpression expression = xPath.compile("expression");
  5. NodeList results = (NodeList) expression.evaluate(new InputSource("data.xml"), XPathConstants.NODESET);
  6. traverse(results);
  7. }
  8.  
  9.  
  10. traverse(NodeList rootNode){
  11. for(int index = 0; index < rootNode.getLength();
  12. index ++){
  13. Node aNode = rootNode.item(index);
  14. if (aNode.getNodeType() == Node.ELEMENT_NODE){
  15. NodeList childNodes = aNode.getChildNodes();
  16. if(aNode.getNodeName().equalsIgnoreCase("address")){
  17. aNode.setTextContent("some address");
  18. }
  19. }
  20. traverse(aNode.getChildNodes());
  21. }
  22. }
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.
Aug 13 '07 #3
jkmyoung
2,057 Expert 2GB
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
Aug 13 '07 #4
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 :)
Aug 14 '07 #5
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
Sep 10 '08 #6
ddau
1
HI I 'm looking for the same code

need to update content node ,using JAva or C# xpath

could you please show the codes
Jul 30 '10 #7
jkmyoung
2,057 Expert 2GB
As stated before, it depends on what you're using to write the node. See post #4 for the link to use XMLOutputter.
Jul 30 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
1
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 >...
2
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...
1
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"...
4
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...
1
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...
2
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>
2
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. ...
8
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...
1
isladogs
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...
0
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...
0
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...
1
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)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.