Warning: Trying to access array offset on value of type bool in /home/feltexco/public_html/felix/wp-content/plugins/google-maps-ready/modules/options/models/options.php on line 16

Warning: Trying to access array offset on value of type bool in /home/feltexco/public_html/felix/wp-content/plugins/google-maps-ready/modules/options/models/options.php on line 16

Warning: Trying to access array offset on value of type bool in /home/feltexco/public_html/felix/wp-content/plugins/google-maps-ready/modules/options/models/options.php on line 16

Warning: Cannot modify header information - headers already sent by (output started at /home/feltexco/public_html/felix/wp-content/plugins/google-maps-ready/modules/options/models/options.php:16) in /home/feltexco/public_html/felix/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home/feltexco/public_html/felix/wp-content/plugins/google-maps-ready/modules/options/models/options.php:16) in /home/feltexco/public_html/felix/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home/feltexco/public_html/felix/wp-content/plugins/google-maps-ready/modules/options/models/options.php:16) in /home/feltexco/public_html/felix/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home/feltexco/public_html/felix/wp-content/plugins/google-maps-ready/modules/options/models/options.php:16) in /home/feltexco/public_html/felix/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home/feltexco/public_html/felix/wp-content/plugins/google-maps-ready/modules/options/models/options.php:16) in /home/feltexco/public_html/felix/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home/feltexco/public_html/felix/wp-content/plugins/google-maps-ready/modules/options/models/options.php:16) in /home/feltexco/public_html/felix/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home/feltexco/public_html/felix/wp-content/plugins/google-maps-ready/modules/options/models/options.php:16) in /home/feltexco/public_html/felix/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home/feltexco/public_html/felix/wp-content/plugins/google-maps-ready/modules/options/models/options.php:16) in /home/feltexco/public_html/felix/wp-includes/rest-api/class-wp-rest-server.php on line 1758
{"id":426,"date":"2014-08-13T11:38:26","date_gmt":"2014-08-13T14:38:26","guid":{"rendered":"http:\/\/www.feltex.com.br\/felix\/?p=426"},"modified":"2014-08-20T13:56:39","modified_gmt":"2014-08-20T16:56:39","slug":"arquivo-xml-java-dom-parser","status":"publish","type":"post","link":"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/","title":{"rendered":"Como criar arquivo XML em Java (DOM Parser)"},"content":{"rendered":"

Como criar arquivo XML em Java (DOM Parser)<\/H1><\/p>\n

Ol\u00e1 Amigos, Hoje veremos como utilizar a Linguagem Java para criar arquivos XML. Existem muitas formas de fazer essas opera\u00e7\u00f5es: desde formas nativas da linguagem (nossa abordagem hoje) ou quanto utilizando frameworks. Em qualquer das abordagens veremos que n\u00e3o s\u00e3o tarefas que demandam grande esfor\u00e7o desde que conhe\u00e7amos a forma mais adequada de manipular esses famosos arquivos XML (eXtensible Markup Language).<\/p>\n

\"XML<\/p>\n


\n Uma das op\u00e7\u00f5es mais interessante \u00e9 a utiliza\u00e7\u00e3o do Framework Criando XML em Java XStream<\/a>. No site oficial h\u00e1 outros exemplos que podem auxiliar voc\u00ea a fazer v\u00e1rias opera\u00e7\u00f5es at\u00e9 mesmo a utiliza\u00e7\u00e3o de arquivos JASON.<\/p>\n

\r\npackage br.com.feltex.webservice.dom;\r\n\r\nimport java.io.File;\r\n\r\nimport javax.xml.parsers.DocumentBuilder;\r\nimport javax.xml.parsers.DocumentBuilderFactory;\r\nimport javax.xml.transform.OutputKeys;\r\nimport javax.xml.transform.Transformer;\r\nimport javax.xml.transform.TransformerFactory;\r\nimport javax.xml.transform.dom.DOMSource;\r\nimport javax.xml.transform.stream.StreamResult;\r\n\r\nimport org.w3c.dom.Document;\r\nimport org.w3c.dom.Element;\r\nimport org.w3c.dom.Node;\r\n\r\npublic class CriaArquivoDOM {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tSystem.out.println(\"Inicio\");\r\n\r\n\t\tDocumentBuilderFactory documentFactory = DocumentBuilderFactory\r\n\t\t\t\t.newInstance();\r\n\t\tDocumentBuilder dBuilder;\r\n\t\ttry {\r\n\t\t\tdBuilder = documentFactory.newDocumentBuilder();\r\n\t\t\tDocument doc = dBuilder.newDocument();\r\n\t\t\t\/\/ Cria o elemento raiz do nosso XML\r\n\t\t\tElement rootElement = doc.createElementNS(\r\n\t\t\t\t\t\"http:\/\/www.feltex.com.br\/aluno\", \"Alunos\");\r\n\t\t\t\/\/ Adiciona um elemento ao documento\r\n\t\t\tdoc.appendChild(rootElement);\r\n\r\n\t\t\t\/\/ Adiciona o primeiro elemento filho ao elemento raiz\r\n\t\t\trootElement.appendChild(criarAluno(doc, \"1\", \"Jose da Silva\", \"55\",\r\n\t\t\t\t\t\"Analista de Sistema\", \"Masculino\"));\r\n\r\n\t\t\t\/\/ Adiciona o segundo elemento filho do elemento raiz\r\n\t\t\trootElement.appendChild(criarAluno(doc, \"2\", \"Maria\", \"22\",\r\n\t\t\t\t\t\"Analista de Banco de Dados\", \"Feminino\"));\r\n\r\n\t\t\t\/\/Cria os objetos que fazem refer\u00eancias \u00e0 console e ao arquivo\r\n\t\t\tTransformerFactory transformerFactory = TransformerFactory\r\n\t\t\t\t\t.newInstance();\r\n\t\t\tTransformer transformer = transformerFactory.newTransformer();\r\n\r\n\t\t\ttransformer.setOutputProperty(OutputKeys.INDENT, \"yes\");\r\n\t\t\tDOMSource source = new DOMSource(doc);\r\n\r\n\t\t\t\r\n\t\t\tStreamResult console = new StreamResult(System.out);\r\n\t\t\tStreamResult file = new StreamResult(new File(\"C:\/temp\/alunos.xml\"));\r\n\r\n\t\t\t\/\/faz a transforma\u00e7\u00e3o dos dados para arquivo e para a console!\r\n\t\t\ttransformer.transform(source, console);\r\n\t\t\ttransformer.transform(source, file);\r\n\t\t\tSystem.out.println(\"Fim\");\r\n\r\n\t\t} catch (Exception e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n\r\n\t\/\/ Criamos todos os atributos como String\r\n\t\/\/ para facilitar a escrita no XML\r\n\tprivate static Node criarAluno(Document doc, String matricula, String nome,\r\n\t\t\tString idade, String cargo, String genero) {\r\n\t\tElement aluno = doc.createElement(\"Aluno\");\r\n\r\n\t\t\/\/ Associa a matr\u00edcula como atributo\r\n\t\taluno.setAttribute(\"id\", matricula);\r\n\r\n\t\t\/\/ Cria o elemento nome\r\n\t\taluno.appendChild(getEmployeeElements(doc, aluno, \"nome\", nome));\r\n\r\n\t\t\/\/ Cria o elemento idade\r\n\t\taluno.appendChild(getEmployeeElements(doc, aluno, \"idade\", idade));\r\n\r\n\t\t\/\/ Cria o elemento cargo\r\n\t\taluno.appendChild(getEmployeeElements(doc, aluno, \"cargo\", cargo));\r\n\r\n\t\t\/\/ Cria o elemtno genero\r\n\t\taluno.appendChild(getEmployeeElements(doc, aluno, \"genero\", genero));\r\n\r\n\t\treturn aluno;\r\n\t}\r\n\r\n\t\/\/ Realiza a cria\u00e7\u00e3o dos elementos texto\r\n\tprivate static Node getEmployeeElements(Document doc, Element elemento,\r\n\t\t\tString nome, String valor) {\r\n\t\tElement node = doc.createElement(nome);\r\n\t\tnode.appendChild(doc.createTextNode(valor));\r\n\t\treturn node;\r\n\t}\r\n}\r\n<\/pre>\n

Conclus\u00e3o<\/H2>
\n Existem maneiras mais f\u00e1ceis de realizar manipula\u00e7\u00e3o de XML em Java, mas est\u00e1 \u00e9 uma alternativa que deve ser conhecida e n\u00e3o pode ser descartada. Uma das vantagens dessa abordagen e n\u00e3o precisar utilizar qualquer framework \u00e9, por exemplo, a forma como trabalhamos com XML em JavaScript ou JQuery. Saber que essa abordagem existe e determinar quando dever\u00e1 ser usada poder\u00e1 ser muito \u00fatil em sua vida profissional.<\/p>\n

Abra\u00e7os e vida que segue!<\/p>\n

Links relacionados<\/H2>
\n
Exemplo utilizando o xStream<\/a>
\n
xStream Site oficial<\/a><\/p>\n

N\u00e3o esque\u00e7a de curtir este post nas rede sociais. D\u00ea a sua contribui\u00e7\u00e3o social e ajude o autor:<\/H2><\/p>\n","protected":false},"excerpt":{"rendered":"

Como criar arquivo XML em Java (DOM Parser) Ol\u00e1 Amigos, Hoje veremos como utilizar a Linguagem Java para criar arquivos XML. Existem muitas formas de fazer essas opera\u00e7\u00f5es: desde formas nativas da linguagem (nossa abordagem hoje) ou quanto utilizando frameworks. …<\/p>\n

Como criar arquivo XML em Java (DOM Parser)<\/span> Read More »<\/a><\/p>\n

<\/p>\n","protected":false},"author":1,"featured_media":1327,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[1],"tags":[],"yoast_head":"\nComo criar arquivo XML em Java (DOM Parser)<\/title>\n<meta name=\"description\" content=\"Como criar arquivo XML em Java (DOM Parser)\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Como criar arquivo XML em Java (DOM Parser)\" \/>\n<meta property=\"og:description\" content=\"Como criar arquivo XML em Java (DOM Parser)\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/\" \/>\n<meta property=\"og:site_name\" content=\"Aprenda Java\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/feltex.br\" \/>\n<meta property=\"article:published_time\" content=\"2014-08-13T14:38:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-08-20T16:56:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.feltex.com.br\/felix\/wp-content\/uploads\/2014\/08\/XML-em-Java-DOM-Parser.png\" \/>\n\t<meta property=\"og:image:width\" content=\"512\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andr\u00e9 F\u00e9lix\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. tempo de leitura\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.feltex.com.br\/felix\/#website\",\"url\":\"https:\/\/www.feltex.com.br\/felix\/\",\"name\":\"Aprenda Java\",\"description\":\"Cursos de java, SQL e Engenharia de Software\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.feltex.com.br\/felix\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"pt-BR\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/#primaryimage\",\"inLanguage\":\"pt-BR\",\"url\":\"https:\/\/www.feltex.com.br\/felix\/wp-content\/uploads\/2014\/08\/XML-em-Java-DOM-Parser.png\",\"contentUrl\":\"https:\/\/www.feltex.com.br\/felix\/wp-content\/uploads\/2014\/08\/XML-em-Java-DOM-Parser.png\",\"width\":512,\"height\":512,\"caption\":\"XML em Java (DOM Parser)\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/#webpage\",\"url\":\"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/\",\"name\":\"Como criar arquivo XML em Java (DOM Parser)\",\"isPartOf\":{\"@id\":\"https:\/\/www.feltex.com.br\/felix\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/#primaryimage\"},\"datePublished\":\"2014-08-13T14:38:26+00:00\",\"dateModified\":\"2014-08-20T16:56:39+00:00\",\"author\":{\"@id\":\"https:\/\/www.feltex.com.br\/felix\/#\/schema\/person\/1e49f842c6254b4561b66ccf573c2069\"},\"description\":\"Como criar arquivo XML em Java (DOM Parser)\",\"breadcrumb\":{\"@id\":\"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Como criar arquivo XML em Java (DOM Parser)\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.feltex.com.br\/felix\/#\/schema\/person\/1e49f842c6254b4561b66ccf573c2069\",\"name\":\"Andr\\u00e9 F\\u00e9lix\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.feltex.com.br\/felix\/#personlogo\",\"inLanguage\":\"pt-BR\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d2d9cc82cab40245e6f803982b1448e6?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d2d9cc82cab40245e6f803982b1448e6?s=96&r=g\",\"caption\":\"Andr\\u00e9 F\\u00e9lix\"},\"sameAs\":[\"http:\/\/www.feltex.com.br\"],\"url\":\"https:\/\/www.feltex.com.br\/felix\/author\/andre.felix\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Como criar arquivo XML em Java (DOM Parser)","description":"Como criar arquivo XML em Java (DOM Parser)","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/","og_locale":"pt_BR","og_type":"article","og_title":"Como criar arquivo XML em Java (DOM Parser)","og_description":"Como criar arquivo XML em Java (DOM Parser)","og_url":"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/","og_site_name":"Aprenda Java","article_publisher":"https:\/\/www.facebook.com\/feltex.br","article_published_time":"2014-08-13T14:38:26+00:00","article_modified_time":"2014-08-20T16:56:39+00:00","og_image":[{"width":512,"height":512,"url":"https:\/\/www.feltex.com.br\/felix\/wp-content\/uploads\/2014\/08\/XML-em-Java-DOM-Parser.png","type":"image\/png"}],"twitter_misc":{"Escrito por":"Andr\u00e9 F\u00e9lix","Est. tempo de leitura":"3 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/www.feltex.com.br\/felix\/#website","url":"https:\/\/www.feltex.com.br\/felix\/","name":"Aprenda Java","description":"Cursos de java, SQL e Engenharia de Software","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.feltex.com.br\/felix\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"pt-BR"},{"@type":"ImageObject","@id":"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/#primaryimage","inLanguage":"pt-BR","url":"https:\/\/www.feltex.com.br\/felix\/wp-content\/uploads\/2014\/08\/XML-em-Java-DOM-Parser.png","contentUrl":"https:\/\/www.feltex.com.br\/felix\/wp-content\/uploads\/2014\/08\/XML-em-Java-DOM-Parser.png","width":512,"height":512,"caption":"XML em Java (DOM Parser)"},{"@type":"WebPage","@id":"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/#webpage","url":"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/","name":"Como criar arquivo XML em Java (DOM Parser)","isPartOf":{"@id":"https:\/\/www.feltex.com.br\/felix\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/#primaryimage"},"datePublished":"2014-08-13T14:38:26+00:00","dateModified":"2014-08-20T16:56:39+00:00","author":{"@id":"https:\/\/www.feltex.com.br\/felix\/#\/schema\/person\/1e49f842c6254b4561b66ccf573c2069"},"description":"Como criar arquivo XML em Java (DOM Parser)","breadcrumb":{"@id":"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.feltex.com.br\/felix\/arquivo-xml-java-dom-parser\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Como criar arquivo XML em Java (DOM Parser)"}]},{"@type":"Person","@id":"https:\/\/www.feltex.com.br\/felix\/#\/schema\/person\/1e49f842c6254b4561b66ccf573c2069","name":"Andr\u00e9 F\u00e9lix","image":{"@type":"ImageObject","@id":"https:\/\/www.feltex.com.br\/felix\/#personlogo","inLanguage":"pt-BR","url":"https:\/\/secure.gravatar.com\/avatar\/d2d9cc82cab40245e6f803982b1448e6?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d2d9cc82cab40245e6f803982b1448e6?s=96&r=g","caption":"Andr\u00e9 F\u00e9lix"},"sameAs":["http:\/\/www.feltex.com.br"],"url":"https:\/\/www.feltex.com.br\/felix\/author\/andre.felix\/"}]}},"_links":{"self":[{"href":"https:\/\/www.feltex.com.br\/felix\/wp-json\/wp\/v2\/posts\/426"}],"collection":[{"href":"https:\/\/www.feltex.com.br\/felix\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.feltex.com.br\/felix\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.feltex.com.br\/felix\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.feltex.com.br\/felix\/wp-json\/wp\/v2\/comments?post=426"}],"version-history":[{"count":11,"href":"https:\/\/www.feltex.com.br\/felix\/wp-json\/wp\/v2\/posts\/426\/revisions"}],"predecessor-version":[{"id":1333,"href":"https:\/\/www.feltex.com.br\/felix\/wp-json\/wp\/v2\/posts\/426\/revisions\/1333"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.feltex.com.br\/felix\/wp-json\/wp\/v2\/media\/1327"}],"wp:attachment":[{"href":"https:\/\/www.feltex.com.br\/felix\/wp-json\/wp\/v2\/media?parent=426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.feltex.com.br\/felix\/wp-json\/wp\/v2\/categories?post=426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.feltex.com.br\/felix\/wp-json\/wp\/v2\/tags?post=426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}