java - Walking through DOM with jdom/dom4j without knowing too much about namespaces -


i got xmp xml jpeg image , want read , update metadata in it. i've written pretty sax parser can read data, realized can't update data it.

so went dom4j. when tried parse xml it, throwed exception unknown namespaces. "direct developer" said need add namespaces manually, if meet image different namespace's url? wouldn't able deal it?

then tried jdom. created document successfully, again, need know namespace url in order child tag it. here quick example of talking about:

document.getrootelement().getchild("rdf", namespace.getnamespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")) 

so, should in order solve problem?

  1. pray url's allways same?
  2. manually update only sax?
  3. find library don't cares namespaces?

here xmp xml example:

<?xpacket begin="" id="w5m0mpcehihzreszntczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="adobe xmp core 5.6-c011 79.156380, 2014/05/21-23:38:37        ">     <rdf:rdf xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">         <rdf:description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:photoshop="http://ns.adobe.com/photoshop/1.0/" xmlns:xmpmm="http://ns.adobe.com/xap/1.0/mm/" xmlns:stevt="http://ns.adobe.com/xap/1.0/stype/resourceevent#" xmlns:exifex="http://cipa.jp/exif/1.0/" dc:format="image/jpeg" xmp:creatortool="image data converter" xmp:modifydate="2015-12-25t22:43:26" xmp:createdate="2015-12-18t17:02:15" xmp:metadatadate="2016-02-03t00:31:43+03:00" photoshop:datecreated="2015-12-18t17:02:15" xmpmm:documentid="8845a702f6819abae2af7d36e005dfdf" xmpmm:originaldocumentid="8845a702f6819abae2af7d36e005dfdf" xmpmm:instanceid="xmp.iid:f27bf752-4ea3-df4c-9c2a-b9837267e444" exifex:photographicsensitivity="800">             <dc:description>                 <rdf:alt>                     <rdf:li xml:lang="x-default">description</rdf:li>                 </rdf:alt>             </dc:description>             <dc:title>                 <rdf:alt>                     <rdf:li xml:lang="x-default">name</rdf:li>                 </rdf:alt>             </dc:title>             <dc:creator>                 <rdf:seq>                     <rdf:li>author</rdf:li>                 </rdf:seq>             </dc:creator>             <dc:subject>                 <rdf:bag>                     <rdf:li>keyword</rdf:li>                     <rdf:li>keywords2</rdf:li>                 </rdf:bag>             </dc:subject>             <xmpmm:history>                 <rdf:seq>                     <rdf:li stevt:action="saved" stevt:instanceid="xmp.iid:b6e2ef4c-c0fe-7549-8c9e-b8b8fe89f1ff" stevt:when="2016-02-02t22:37:17+03:00" stevt:softwareagent="adobe photoshop camera raw 9.4" stevt:changed="/metadata"/>                     <rdf:li stevt:action="saved" stevt:instanceid="xmp.iid:f27bf752-4ea3-df4c-9c2a-b9837267e444" stevt:when="2016-02-03t00:31:43+03:00" stevt:softwareagent="adobe photoshop camera raw 9.4 (windows)" stevt:changed="/metadata"/>                 </rdf:seq>             </xmpmm:history>         </rdf:description>     </rdf:rdf> </x:xmpmeta> <?xpacket end="w"?> 

none of options help:

  1. if namespace url change means new feature introduced or semantics of tags or attributes changed. need understand these changes in order make edit doesn't break anything. praying might of course if specific deity has power induce knowledge.

  2. parsing sax doesn't make namespace problem go away, , doesn't support updating document.

  3. using namespace-unaware parser worst option. screw things if namespace urls change.

to summarize, navigation , edits/updates need namespace aware. however, can limited editing if navigate xpath. example, if want add list iten <bag> element youd this:

string expression = "//*[local-name() = 'subject']/*[local-name() = 'bag']"; element bag = (element) xpathfactory.instance().compile().evaluatefirst(document); element listitem = new element("li", bag.getnamespace()).settext("..."); bag.addcontent(listitem); 

this example written out of memory , untested, might not compile.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -