Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 418 | espaco | 1 | package br.com.ec.shared; |
| 2 | |||
| 3 | import org.jdom2.Attribute; |
||
| 4 | import org.jdom2.Document; |
||
| 5 | import org.jdom2.Element; |
||
| 6 | import org.jdom2.JDOMException; |
||
| 7 | import org.jdom2.input.SAXBuilder; |
||
| 8 | import org.jdom2.output.Format; |
||
| 9 | import org.jdom2.output.XMLOutputter; |
||
| 10 | |||
| 11 | import java.io.IOException; |
||
| 12 | import java.io.StringReader; |
||
| 13 | import java.io.StringWriter; |
||
| 14 | import java.util.List; |
||
| 15 | |||
| 16 | public class XmlComponente { |
||
| 17 | |||
| 18 | public static Document convertStringToXml(String xmlString) { |
||
| 19 | SAXBuilder sax = new SAXBuilder(); |
||
| 20 | // sax.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); |
||
| 21 | // sax.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); |
||
| 22 | try { |
||
| 23 | Document doc = sax.build(new StringReader(xmlString)); |
||
| 24 | return doc; |
||
| 25 | } catch (JDOMException | IOException e) { |
||
| 26 | throw new RuntimeException(e); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | public static String convertXmlDocumentToString(Document doc) { |
||
| 31 | XMLOutputter xmlOutput = new XMLOutputter(); |
||
| 32 | xmlOutput.setFormat(Format.getPrettyFormat().setEncoding("UTF-8")); |
||
| 33 | //xmlOutput.setFormat(Format.getPrettyFormat()); |
||
| 34 | StringWriter result = new StringWriter(); |
||
| 35 | try { |
||
| 36 | xmlOutput.output(doc, result); |
||
| 37 | } catch (IOException e) { |
||
| 38 | throw new RuntimeException(e); |
||
| 39 | } |
||
| 40 | return result.toString(); |
||
| 41 | } |
||
| 42 | |||
| 43 | public static void exibirFilhos(Element elemento) { |
||
| 44 | List<Element> filhosElementos = elemento.getChildren(); |
||
| 45 | for (Element elementoFilho : filhosElementos) { |
||
| 46 | exibirAtributos(elementoFilho); |
||
| 47 | if (!elementoFilho.getChildren().isEmpty()) { |
||
| 48 | exibirFilhos(elementoFilho); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | public static void exibirAtributos(Element elemento) { |
||
| 54 | System.out.println("- " + elemento.getName() + " = " + elemento.getValue()); |
||
| 55 | for (Attribute atributo : elemento.getAttributes()) { |
||
| 56 | System.out.println(atributo.getName() + " = " + atributo.getValue()); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | } |