Supprimer les namespaces dans un fichier XML
Bonjour à tous,
j'utilise Stax pour parser un fichier XML( le but est de deviser un grand fichier XML en plusieurs petits fichiers XML) voici le code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| private void split(String xmlResource, String condition) throws Exception {
XMLEventFactory xef = XMLEventFactory.newFactory();
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLEventReader xer = xif.createXMLEventReader(new FileReader(xmlResource));
StartDocument startDocument = xef.createStartDocument();
while(xer.hasNext() && !xer.peek().isEndDocument()) {
boolean metCondition;
XMLEvent xmlEvent = xer.nextTag();
if(!xmlEvent.isStartElement()) {
break;
}
StartElement breakStartElement = xmlEvent.asStartElement();
List<XMLEvent> cachedXMLEvents = new ArrayList<XMLEvent>();
List<String> cachedXMLEvent = new ArrayList<String>();
if(null == condition) {
cachedXMLEvents.add(breakStartElement);
metCondition = true;
} else {
cachedXMLEvents.add(breakStartElement);
xmlEvent = xer.nextEvent();
metCondition = false;
while(!(xmlEvent.isEndElement() && xmlEvent.asEndElement().getName().equals(breakStartElement.getName()))) {
cachedXMLEvents.add(xmlEvent);
cachedXMLEvent.add(xmlEvent.toString());
if(xmlEvent.isStartElement() && xmlEvent.asStartElement().getName().getLocalPart().equals(condition)) {
metCondition = true;
break;
}
xmlEvent = xer.nextEvent();
}
}
cachedXMLEvents.add(xmlEvent);
metCondition = true;
if(metCondition) {
// Create a file for the fragment, the name is derived from the value of the id attribute
FileWriter fileWriter = new FileWriter("c:/test.xml");
DocumentBuilderFactory dbFactory_ = DocumentBuilderFactory.newInstance();
dbFactory_.setNamespaceAware(false);
Document doc_;
DocumentBuilder dBuilder = dbFactory_.newDocumentBuilder();
String temp=startDocument.toString();
// Write the XMLEvents that were cached while when we were
// checking the fragment to see if it matched our criteria.
for(XMLEvent cachedEvent : cachedXMLEvents) {
temp+=cachedEvent.toString();
}
}
}
} |
le traitement fonctionne correctement, mais à la fin dans la variable "temp" je me trouve avec un fichier xml avec des namespaces dans tous les élements, exemple:
Citation:
<?xml version="1.0" encoding='UTF-8'?>
<['http://www.tibco.com/schemas/splitter/Resources/Schema.xsd']::staff>
<['http://www.tibco.com/schemas/splitter/Resources/Schema.xsd']::firstname>yong</['http://www.tibco.com/schemas/splitter/Resources/Schema.xsd']::firstname>
<['http://www.tibco.com/schemas/splitter/Resources/Schema.xsd']::lastname>mook kim</['http://www.tibco.com/schemas/splitter/Resources/Schema.xsd']::lastname>
<['http://www.tibco.com/schemas/splitter/Resources/Schema.xsd']::nickname>mkyong</['http://www.tibco.com/schemas/splitter/Resources/Schema.xsd']::nickname>
<['http://www.tibco.com/schemas/splitter/Resources/Schema.xsd']::salary>100000</['http://www.tibco.com/schemas/splitter/Resources/Schema.xsd']::salary>
</['http://www.tibco.com/schemas/splitter/Resources/Schema.xsd']::staff>
comment faire pour supprimer les namespaces ? je ne veux pas écrire le résultat dans un fichier en utilisant XMLEventWriter, car je souhaite envoyer le String via JMS.
merci de votre aide.