|
30 | 30 | import org.javalite.common.Escape;
|
31 | 31 | import org.slf4j.Logger;
|
32 | 32 | import org.slf4j.LoggerFactory;
|
33 |
| -import org.w3c.dom.Document; |
34 |
| -import org.w3c.dom.Element; |
35 |
| -import org.w3c.dom.Node; |
36 |
| -import org.w3c.dom.NodeList; |
37 |
| - |
38 |
| -import javax.xml.parsers.DocumentBuilderFactory; |
| 33 | +import javax.xml.stream.XMLInputFactory; |
| 34 | +import javax.xml.stream.XMLStreamConstants; |
| 35 | +import javax.xml.stream.XMLStreamException; |
| 36 | +import javax.xml.stream.XMLStreamReader; |
39 | 37 | import java.io.*;
|
40 | 38 | import java.math.BigDecimal;
|
41 | 39 | import java.sql.Clob;
|
|
45 | 43 | import java.util.*;
|
46 | 44 |
|
47 | 45 | import static org.javalite.common.Inflector.*;
|
| 46 | +import static org.javalite.common.Util.blank; |
48 | 47 | import static org.javalite.common.Util.empty;
|
49 | 48 | import static org.javalite.common.Util.join;
|
50 | 49 |
|
@@ -798,29 +797,31 @@ public String toString() {
|
798 | 797 | * @param xml xml to read model attributes from.
|
799 | 798 | */
|
800 | 799 | public void fromXml(String xml) {
|
801 |
| - |
802 |
| - try{ |
803 |
| - //such dumb API! |
804 |
| - Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes())); |
805 |
| - String topTag = underscore(getClass().getSimpleName()); |
806 |
| - Element root = document.getDocumentElement(); |
807 |
| - |
808 |
| - if(!root.getTagName().equals(topTag)){ |
809 |
| - throw new InitException("top node has to match model name: " + topTag); |
810 |
| - } |
811 |
| - NodeList childNodes = root.getChildNodes(); |
812 |
| - |
813 |
| - Map<String, String> attributesMap = new HashMap<String, String>(); |
814 |
| - for(int i = 0; i < childNodes.getLength();i++){ |
815 |
| - Node node = childNodes.item(i); |
816 |
| - if(node instanceof Element){ |
817 |
| - Element child = (Element) node; |
818 |
| - attributesMap.put(child.getTagName(), child.getFirstChild().getNodeValue());//this is even dumber! |
| 800 | + try { |
| 801 | + XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new ByteArrayInputStream(xml.getBytes())); |
| 802 | + String attr = null; |
| 803 | + String chars = null; |
| 804 | + Map<Object, Object> res = new HashMap<Object, Object>(); |
| 805 | + while (reader.hasNext()) { |
| 806 | + int event = reader.next(); |
| 807 | + switch (event) { |
| 808 | + case XMLStreamConstants.START_ELEMENT: |
| 809 | + attr = reader.getLocalName(); |
| 810 | + break; |
| 811 | + case XMLStreamConstants.CHARACTERS: |
| 812 | + chars = reader.getText().trim();; |
| 813 | + break; |
| 814 | + case XMLStreamConstants.END_ELEMENT: |
| 815 | + if (attr != null && !blank(chars)) { |
| 816 | + res.put(attr, chars); |
| 817 | + } |
| 818 | + attr = chars = null; |
| 819 | + break; |
819 | 820 | }
|
820 | 821 | }
|
821 |
| - fromMap(attributesMap); |
822 |
| - }catch(Exception e){ |
823 |
| - throw new InitException(e); |
| 822 | + fromMap(res); |
| 823 | + } catch (XMLStreamException e) { |
| 824 | + throw new InitException(e); |
824 | 825 | }
|
825 | 826 | }
|
826 | 827 |
|
|
0 commit comments