Blame |
Last modification |
View Log
| Download
| RSS feed
package br.com.ec.shared;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
public class XmlComponente
{
public static Document convertStringToXml
(String xmlString
) {
SAXBuilder sax =
new SAXBuilder
();
// sax.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
// sax.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
try {
Document doc = sax.
build(new StringReader(xmlString
));
return doc
;
} catch (JDOMException |
IOException e
) {
throw new RuntimeException(e
);
}
}
public static String convertXmlDocumentToString
(Document doc
) {
XMLOutputter xmlOutput =
new XMLOutputter
();
xmlOutput.
setFormat(Format.
getPrettyFormat().
setEncoding("UTF-8"));
//xmlOutput.setFormat(Format.getPrettyFormat());
StringWriter result =
new StringWriter();
try {
xmlOutput.
output(doc, result
);
} catch (IOException e
) {
throw new RuntimeException(e
);
}
return result.
toString();
}
public static void exibirFilhos
(Element elemento
) {
List<Element> filhosElementos = elemento.
getChildren();
for (Element elementoFilho : filhosElementos
) {
exibirAtributos
(elementoFilho
);
if (!elementoFilho.
getChildren().
isEmpty()) {
exibirFilhos
(elementoFilho
);
}
}
}
public static void exibirAtributos
(Element elemento
) {
System.
out.
println("- " + elemento.
getName() +
" = " + elemento.
getValue());
for (Attribute atributo : elemento.
getAttributes()) {
System.
out.
println(atributo.
getName() +
" = " + atributo.
getValue());
}
}
}