0

I got some xmp XML from JPEG image and I want to read and update metadata in it. I've written a pretty SAX parser which can read data, but then I realized that I can't update data with it.

So I went to dom4j. When I tried to parse my XML with it, it throwed an exception about unknown namespaces. A "direct developer" said that I just need to add those namespaces manually, but what if I meet an image with different namespace's url? I wouldn't be able to deal with it?

Then I tried jdom. It created document successfully, but again, I need to know a namespace url in order to get a child tag with it. Here is a quick example of what I am talking about:

document.getRootElement().getChild("RDF", Namespace.getNamespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#"))

So, what should I do in order to solve my problem?

  1. Pray about url's will allways be the same?
  2. Manually update with only sax?
  3. Find a library which don't cares about namespaces?

Here is a an 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"?>
Community
  • 1
  • 1
sinedsem
  • 5,413
  • 7
  • 29
  • 46

1 Answers1

2

None of your options will help:

  1. If the namespace URL change it means that a new feature was introduced or that the semantics of the tags or attributes changed. You need to understand these changes in order to make an edit that doesn't break anything. Praying might of course help if your specific deity has the power to induce you with this knowledge.

  2. Parsing with SAX doesn't make the namespace problem go away, and it doesn't really support updating a document.

  3. Using a namespace-unaware parser is probably the worst option. THen you will really screw things up if the namespace URLs change.

To summarize, all your navigation and edits/updates need to be namespace aware. However, you can do limited editing if you navigate with XPath. For example, if you want to add a list iten to the <Bag> element youd could do something like 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 is written out of memory and untested, so it might not even compile.

forty-two
  • 12,204
  • 2
  • 26
  • 36