I have an xml and I am trying to unmarshal. It fails because it is missing the require namespaces.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tracks>
<tracklet><sightings/></tracklet>
<tracks>
Needs to become:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:tracks xmlns:ns3="http://www.mytrack.com/TRACK" xmlns:xmime="http://www.w3.org/2005/05/xmlmime">
<tracklet><sightings/></tracklet>
</ns3:tracks>
This example of NamespaceFilter only does one namespace. I need one that will append two namespaces.
public class NamespaceFilter extends XMLFilterImpl {
private static final String NAMESPACE = "http://www.example.com/customer";
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(NAMESPACE, localName, qName);
}
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
super.startElement(NAMESPACE, localName, qName, atts);
}
}
The NamespaceFilter
in your question doesn't "add" the namespace declaration, it adjusts the namespace portion of an elements qualified name.
For your XML you just need to adjust the NamespaceFilter
so that it only returns the namespace when the qname
parameter is tracks
.
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if("tracks".equals(qname) {
super.startElement(NAMESPACE, localName, qName, atts);
} else {
super.startElement(uri, localName, qName, atts);
}
}
Here is the link to my blog post where the NamespaceFilter
came from:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.