Hi
Can I in this way delete, add or modify an element or attribute?
Thanks
-----Original Message-----
From: Hank Ratzesberger [mailto:xml_at_xmlwerks.com]
Sent: den 1 juli 2013 17:59
To: users_at_jaxb.java.net
Subject: Re: XPATH and JAXB
Hi BR,
I think what you are looking for is the JAXB binder method, that can map a JAXB element to a document, then you can use XPath on the doc, then the binder will sync the changes:
try {
JAXBContext jaxbContext = JAXBContext.newInstance( com.blah.blah.MyType.class );
QName qname = new QName("
http://www.example.com/schema", "myObject"); Binder<Node> binder = jaxbContext.createBinder(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//dbf.setNamespaceAware(false); // This was tested and does not work, needs a namespace context dbf.setValidating(false); DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
//Binder will maintains association between two views.
binder.marshal( new JAXBElement<MyType>(qname, com.blah.blah.MyType.class, MyType), doc );
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext( new MyNamespaceContext() );
// Example xpath
//Node node = (Node) xpath.evaluate("/ns:thing", doc, XPathConstants.NODE );
Node n = (Node) xpath.evaluate( path, doc, XPathConstants.NODE );
if (n != null) {
n.setTextContent( text );
}
// And here is where you sync things up
binder.updateJAXB( doc );
} catch( Exception e ) {
//e.printStackTrace();
log.error( e.getClass() + ": " + e.getMessage() ); return null; } // These are the specific exceptions:
//catch (JAXBException e) {
// e.printStackTrace();
//} catch (XPathExpressionException e) { // e.printStackTrace(); //} catch (ParserConfigurationException e) { // e.printStackTrace(); //} catch (TransformerException e) { // e.printStackTrace(); //} //
Regards,
Hank
On Mon, Jul 1, 2013 at 1:30 AM, Lulseged Zerfu <lulseged.zerfu_at_ericsson.com> wrote:
> Hi
>
>
>
> I started to use JAXB and everything works fine. Now I need to add,
> modify or delete element or attribute and I have to use XPATH.
>
>
>
> Does JAXB works with XPATH? Or are there any other implementation?
>
>
>
> Any road map in mind?
>
>
>
> Any example?
>
>
>
> BR
>
> Lulseged
--
Hank Ratzesberger
XMLWerks.com