Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 37569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:34:16+00:00 2026-05-10T14:34:16+00:00

I have a XmlDocument in java, created with the Weblogic XmlDocument parser. I want

  • 0

I have a XmlDocument in java, created with the Weblogic XmlDocument parser.

I want to replace the content of a tag in this XMLDocument with my own data, or insert the tag if it isn’t there.

<customdata>    <tag1 />    <tag2>mfkdslmlfkm</tag2>    <location />    <tag3 /> </customdata> 

For example I want to insert a URL in the location tag:

<location>http://something</location> 

but otherwise leave the XML as is.

Currently I use a XMLCursor:

    XmlObject xmlobj = XmlObject.Factory.parse(a.getCustomData(), options);     XmlCursor xmlcur = xmlobj.newCursor();      while (xmlcur.hasNextToken()) {       boolean found = false;       if (xmlcur.isStart() && 'schema-location'.equals(xmlcur.getName().toString())) {         xmlcur.setTextValue('http://replaced');         System.out.println('replaced');         found = true;       } else if (xmlcur.isStart() && 'customdata'.equals(xmlcur.getName().toString())) {         xmlcur.push();       } else if (xmlcur.isEnddoc()) {         if (!found) {           xmlcur.pop();           xmlcur.toEndToken();           xmlcur.insertElementWithText('schema-location', 'http://inserted');           System.out.println('inserted');         }        }       xmlcur.toNextToken();     } 

I tried to find a ‘quick’ xquery way to do this since the XmlDocument has an execQuery method, but didn’t find it very easy.

Do anyone have a better way than this? It seems a bit elaborate.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:34:17+00:00Added an answer on May 10, 2026 at 2:34 pm

    How about an XPath based approach? I like this approach as the logic is super-easy to understand. The code is pretty much self-documenting.

    If your xml document is available to you as an org.w3c.dom.Document object (as most parsers return), then you could do something like the following:

    // get the list of customdata nodes NodeList customDataNodeSet = findNodes(document, '//customdata' );  for (int i=0 ; i < customDataNodeSet.getLength() ; i++) {   Node customDataNode = customDataNodeSet.item( i );    // get the location nodes (if any) within this one customdata node   NodeList locationNodeSet = findNodes(customDataNode, 'location' );    if (locationNodeSet.getLength() > 0) {     // replace     locationNodeSet.item( 0 ).setTextContent( 'http://stackoverflow.com/' );   }   else {     // insert     Element newLocationNode = document.createElement( 'location' );     newLocationNode.setTextContent('http://stackoverflow.com/' );     customDataNode.appendChild( newLocationNode );   } } 

    And here’s the helper method findNodes that does the XPath search.

    private NodeList findNodes( Object obj, String xPathString )   throws XPathExpressionException {    XPath xPath = XPathFactory.newInstance().newXPath();   XPathExpression expression = xPath.compile( xPathString );   return (NodeList) expression.evaluate( obj, XPathConstants.NODESET ); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 116k
  • Answers 116k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer 0x3a009598 would be the address of the x86 instruction that… May 11, 2026 at 10:38 pm
  • Editorial Team
    Editorial Team added an answer I think this syntax works here: from stack s where… May 11, 2026 at 10:38 pm
  • Editorial Team
    Editorial Team added an answer I believe the only thing you'd need to change when… May 11, 2026 at 10:38 pm

Related Questions

I am a bit rusty in my Java, and this is my first time
I'm looking to create XML Document objects in Java and serialize them to a
I need to build an XML document from a Java object hierarchy. Both the
Is there is Simple way to read and write Xml in Java? I've used

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.