Guest User

Untitled

a guest
Jan 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. package br.com.syspdv.synch.server.model.layout.importacao.xml;
  2.  
  3. import java.io.FileInputStream;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import javax.xml.namespace.QName;
  10. import javax.xml.stream.XMLInputFactory;
  11. import javax.xml.stream.XMLStreamReader;
  12.  
  13. import org.apache.log4j.Logger;
  14.  
  15. public class SysPDVStAXReader {
  16.  
  17. private static Logger logger = Logger.getLogger(SysPDVStAXReader.class);
  18.  
  19. private final String xmlFilePath;
  20.  
  21. public SysPDVStAXReader(String xmlFilePath) {
  22. this.xmlFilePath = xmlFilePath;
  23. }
  24.  
  25. public List<Map<String, Object>> getItems() {
  26.  
  27. List<Map<String, Object>> rows = new ArrayList<Map<String,Object>>();
  28.  
  29. try {
  30.  
  31. XMLInputFactory factory = XMLInputFactory.newFactory();
  32. XMLStreamReader parser = factory.createXMLStreamReader(new FileInputStream(xmlFilePath), "Cp1252");
  33.  
  34. String currentTag = null;
  35. Map<String, Object> currentRow = null;
  36.  
  37. while (parser.hasNext()) {
  38.  
  39. int currentEvent = parser.next();
  40. switch (currentEvent) {
  41. case XMLStreamReader.START_ELEMENT:
  42.  
  43. currentTag = parser.getLocalName();
  44.  
  45. if ("ROW".equals(currentTag)) {
  46. currentRow = new HashMap<String, Object>(65); // numero medio de atributos de produtos
  47.  
  48. int count = parser.getAttributeCount();
  49. if (count > 0) {
  50. for (int i=0; i < count; i++) {
  51.  
  52. QName name = parser.getAttributeName(i);
  53. String value = parser.getAttributeValue(i);
  54.  
  55. currentRow.put(name.toString(), value);
  56. }
  57. }
  58.  
  59. }
  60.  
  61. break;
  62. case XMLStreamReader.END_ELEMENT:
  63.  
  64. currentTag = parser.getLocalName();
  65.  
  66. if ("ROW".equals(currentTag)) {
  67. rows.add(currentRow);
  68. }
  69.  
  70. break;
  71. }
  72.  
  73. }
  74.  
  75.  
  76. } catch (Exception e) {
  77. logger.error("Erro ao processar XML " + xmlFilePath + ": " + e.getMessage());
  78. throw new IllegalStateException(e);
  79. }
  80.  
  81. return rows;
  82. }
  83. }
Add Comment
Please, Sign In to add comment