1

I've made code that sends an XML request to a server and delivers XML back to me (with nodes and all) and I was wondering how to go about using XPath to strip down the XML to just the content. I have it working right now where I the returned string is saved into a external document, and then the Xpath reads the external document. But I'd like to cut out this process down.

Any ideas?

1 Answer 1

1

Many XML libraries allow for String manipulation of XML & XPATH.

There are examples with dom4j here.

From the link provided:

String text = "<person> <name>James</name><address><city>New York</city></address></person><person> <name>Billy</name> </person>";
Document document = DocumentHelper.parseText(text);

List list = document.selectNodes( "//person/name" );
Node node = document.selectSingleNode( "//person/address/city" );
Sign up to request clarification or add additional context in comments.

4 Comments

Could you explain the @name ? It seems strange because I am used to @ meaning attribute. But in the string, provided, name is not an attribute.
I just copied it from the 'Powerful Navigation with XPath' example, there isn't really any text that matches it. You're right it matches on attribute. I will remove it, it is confusing and doesn't add anything.
Just out of curiosity, where do I put the library after I download it? I'm using Eclipse and I'm not sure how to properly import it!
I would suggest taking the time to learn and use maven. Aside from that, you have to download it, drag it into your project from your download location, right click and select "add to build path". That should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.