{"id":287,"date":"2012-11-11T19:23:49","date_gmt":"2012-11-11T19:23:49","guid":{"rendered":"http:\/\/ilias-laptop\/examples\/core-java\/xml\/dom\/add-attribute-in-dom-element\/"},"modified":"2013-07-06T16:44:27","modified_gmt":"2013-07-06T13:44:27","slug":"add-attribute-in-dom-element","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/","title":{"rendered":"Add attribute in DOM element"},"content":{"rendered":"<p>This is an example of how to add an attribute in a DOM element.\tWe have implemented a method, that is <code>void prettyPrint(Document xml)<\/code>, in order to convert a DOM into a formatted XML String. Adding an attribute in a DOM element implies that you should:<\/p>\n<ul>\n<li>Obtain a new instance of a <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/xml\/parsers\/DocumentBuilderFactory.html\" target=\"_blank\">DocumentBuilderFactory<\/a>, that is a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents. <\/li>\n<li>Set the parser produced so as not to validate documents as they are parsed, using <code>setValidating(boolean validating)<\/code> API method of DocumentBuilderFactory, with validating set to false. <\/li>\n<li>Create a new instance of a <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/xml\/parsers\/DocumentBuilder.html\" target=\"_blank\">DocumentBuilder<\/a>, using <code>newDocumentBuilder()<\/code> API method of DocumentBuilderFactory. <\/li>\n<li>Parse the <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/io\/FileInputStream.html\" target=\"_blank\">FileInputStream<\/a> with the content to be parsed, using <code>parse(InputStream is)<\/code> API method of DocumentBuilder. This method parses the content of the given InputStream as an XML document and returns a new DOM <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/org\/w3c\/dom\/Document.html\" target=\"_blank\">Document<\/a> object. <\/li>\n<li>Get the <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/org\/w3c\/dom\/NodeList.html\" target=\"_blank\">NodeList<\/a> of all the <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/org\/w3c\/dom\/Element.html\" target=\"_blank\">Element<\/a> objects in document order with a given tag name and are contained in the document using <code>getElementsByTagName(String tagname)<\/code> API method of <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/org\/w3c\/dom\/Document.html\" target=\"_blank\">Document<\/a> and from this nodeList get the first element.<\/li>\n<li>Add a new attribute to the element, using <code>setAttribute(String name, String value)<\/code>. <\/li>\n<li>Call <code>void prettyPrint(Document xml)<\/code> method of the example. The method gets the xml Document and converts it into a formatted xml String, after transforming it with specific parameters, such as encoding. The method uses a <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/xml\/transform\/Transformer.html\" target=\"_blank\">Transformer<\/a>, that is created using <code>newTransformer()<\/code> API method of <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/xml\/transform\/TransformerFactory.html\" target=\"_blank\">TransformerFactory<\/a>. The Transformer is used to transform a source tree into a result tree. After setting specific output properties to the transformer, using <code>setOutputProperty(String name, String value)<\/code> API method of Transformer, the method uses it to make the transformation, with <code>transform(Source xmlSource, Result outputTarget)<\/code> API method of Transformer. The parameters are the <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/xml\/transform\/dom\/DOMSource.html\" target=\"_blank\">DOMSource<\/a> with the DOM node and the result that is a <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/xml\/transform\/stream\/StreamResult.html\" target=\"_blank\">StreamResult<\/a> created from a <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/io\/StringWriter.html\" target=\"_blank\">StringWriter<\/a>.<\/li>\n<\/ul>\n<p>Let\u2019s take a look at the code snippet that follows:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">\r\npackage com.javacodegeeks.snippets.core;\r\n\r\nimport java.io.File;\r\nimport java.io.FileInputStream;\r\nimport java.io.StringWriter;\r\nimport java.io.Writer;\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\n\r\npublic class AddAttributeInDOMElement {\r\n\r\n\tpublic static void main(String[] args) throws Exception {\r\n\t\t\r\n\t\tDocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r\n\t\tdbf.setValidating(false);\r\n\t\tDocumentBuilder db = dbf.newDocumentBuilder();\r\n\t\t\r\n\t\tDocument doc = db.parse(new FileInputStream(new File(\"in.xml\")));\r\n\t\t\r\n\t\tElement element = (Element) doc.getElementsByTagName(\"channel\").item(0);\r\n\r\n\t\t\/\/ Adds a new attribute. If an attribute with that name is already present \r\n\t    \/\/ in the element, its value is changed to be that of the value parameter\r\n\t\telement.setAttribute(\"newattr\", \"attrvalue\");\r\n\t\t\r\n\t\tprettyPrint(doc);\r\n\t\t\r\n\t\t\/\/ whether an attribute with a given name is specified on this element or has a default value\r\n\t\tboolean hasAttribute = element.hasAttribute(\"newattr\");\r\n\t\tSystem.out.println(\"Attribute Added: \" + hasAttribute);\r\n\t\t\r\n\t}\r\n\t\r\n\tpublic static final void prettyPrint(Document xml) throws Exception {\r\n\t\tTransformer tf = TransformerFactory.newInstance().newTransformer();\r\n\t\ttf.setOutputProperty(OutputKeys.ENCODING, \"UTF-8\");\r\n\t\ttf.setOutputProperty(OutputKeys.INDENT, \"yes\");\r\n\t\tWriter out = new StringWriter();\r\n\t\ttf.transform(new DOMSource(xml), new StreamResult(out));\r\n\t\tSystem.out.println(out.toString());\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>\n<b>Input:<\/b><\/p>\n<pre class=\"brush:xml\">\r\n&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;rss version=\"2.0\"&gt;\r\n\t&lt;channel&gt;\r\n\t\t&lt;title&gt;Java Tutorials and Examples&lt;\/title&gt;\r\n\t\t&lt;item&gt;\r\n\t\t\t&lt;title&gt;&lt;![CDATA[Java Tutorials]]&gt;&lt;\/title&gt;\r\n\t\t\t&lt;link&gt;http:\/\/www.javacodegeeks.com\/&lt;\/link&gt;\r\n\t\t&lt;\/item&gt;\r\n\t\t&lt;item&gt;\r\n\t\t\t&lt;title&gt;&lt;![CDATA[Java Examples]]&gt;&lt;\/title&gt;\r\n\t\t\t&lt;link&gt;http:\/\/examples.javacodegeeks.com\/&lt;\/link&gt;\r\n\t\t&lt;\/item&gt;\r\n\t&lt;\/channel&gt;\r\n&lt;\/rss&gt;\r\n<\/pre>\n<p>\n<b>Output:<\/b><\/p>\n<pre style=\"background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;\">\r\n<code style=\"color: black; word-wrap: normal;\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?&gt;\r\n&lt;rss version=\"2.0\"&gt;\r\n\t&lt;channel newattr=\"attrvalue\"&gt;\r\n\t\t&lt;title&gt;Java Tutorials and Examples&lt;\/title&gt;\r\n\t\t&lt;item&gt;\r\n\t\t\t&lt;title&gt;&lt;![CDATA[Java Tutorials]]&gt;&lt;\/title&gt;\r\n\t\t\t&lt;link&gt;http:\/\/www.javacodegeeks.com\/&lt;\/link&gt;\r\n\t\t&lt;\/item&gt;\r\n\t\t&lt;item&gt;\r\n\t\t\t&lt;title&gt;&lt;![CDATA[Java Examples]]&gt;&lt;\/title&gt;\r\n\t\t\t&lt;link&gt;http:\/\/examples.javacodegeeks.com\/&lt;\/link&gt;\r\n\t\t&lt;\/item&gt;\r\n\t&lt;\/channel&gt;\r\n&lt;\/rss&gt;\r\n\r\nAttribute Added: true<\/code>\r\n<\/pre>\n<p>&nbsp;<br \/>\nThis was an example of how to add an attribute in a DOM element in Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is an example of how to add an attribute in a DOM element. We have implemented a method, that is void prettyPrint(Document xml), in order to convert a DOM into a formatted XML String. Adding an attribute in a DOM element implies that you should: Obtain a new instance of a DocumentBuilderFactory, that is &hellip;<\/p>\n","protected":false},"author":6,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[189,197,1066],"class_list":["post-287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dom","tag-core-java-2","tag-dom-2","tag-xml"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Add attribute in DOM element - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This is an example of how to add an attribute in a DOM element. We have implemented a method, that is void prettyPrint(Document xml), in order to convert\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Add attribute in DOM element - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This is an example of how to add an attribute in a DOM element. We have implemented a method, that is void prettyPrint(Document xml), in order to convert\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2012-11-11T19:23:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-07-06T13:44:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Byron Kiourtzoglou\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Byron Kiourtzoglou\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/\"},\"author\":{\"name\":\"Byron Kiourtzoglou\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/3b111ec1048740c68c9e709ff6240015\"},\"headline\":\"Add attribute in DOM element\",\"datePublished\":\"2012-11-11T19:23:49+00:00\",\"dateModified\":\"2013-07-06T13:44:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/\"},\"wordCount\":318,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"keywords\":[\"core java\",\"dom\",\"xml\"],\"articleSection\":[\"DOM\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/\",\"name\":\"Add attribute in DOM element - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2012-11-11T19:23:49+00:00\",\"dateModified\":\"2013-07-06T13:44:27+00:00\",\"description\":\"This is an example of how to add an attribute in a DOM element. We have implemented a method, that is void prettyPrint(Document xml), in order to convert\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"Bipartite Graph\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"xml\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/xml\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"DOM\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/xml\/dom\/\"},{\"@type\":\"ListItem\",\"position\":6,\"name\":\"Add attribute in DOM element\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/3b111ec1048740c68c9e709ff6240015\",\"name\":\"Byron Kiourtzoglou\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/10\/Byron-Kiourtzoglou-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/10\/Byron-Kiourtzoglou-96x96.jpg\",\"caption\":\"Byron Kiourtzoglou\"},\"description\":\"Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications\/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.\",\"sameAs\":[\"https:\/\/www.pivotalgamers.com\/\",\"https:\/\/www.linkedin.com\/in\/byron-kiourtzoglou-530ab222\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/byron-kiourtzoglou\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Add attribute in DOM element - Java Code Geeks","description":"This is an example of how to add an attribute in a DOM element. We have implemented a method, that is void prettyPrint(Document xml), in order to convert","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:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/","og_locale":"en_US","og_type":"article","og_title":"Add attribute in DOM element - Java Code Geeks","og_description":"This is an example of how to add an attribute in a DOM element. We have implemented a method, that is void prettyPrint(Document xml), in order to convert","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2012-11-11T19:23:49+00:00","article_modified_time":"2013-07-06T13:44:27+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Byron Kiourtzoglou","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Byron Kiourtzoglou","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/"},"author":{"name":"Byron Kiourtzoglou","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/3b111ec1048740c68c9e709ff6240015"},"headline":"Add attribute in DOM element","datePublished":"2012-11-11T19:23:49+00:00","dateModified":"2013-07-06T13:44:27+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/"},"wordCount":318,"commentCount":1,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["core java","dom","xml"],"articleSection":["DOM"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/","name":"Add attribute in DOM element - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2012-11-11T19:23:49+00:00","dateModified":"2013-07-06T13:44:27+00:00","description":"This is an example of how to add an attribute in a DOM element. We have implemented a method, that is void prettyPrint(Document xml), in order to convert","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/dom\/add-attribute-in-dom-element\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"xml","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/xml\/"},{"@type":"ListItem","position":5,"name":"DOM","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/xml\/dom\/"},{"@type":"ListItem","position":6,"name":"Add attribute in DOM element"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/3b111ec1048740c68c9e709ff6240015","name":"Byron Kiourtzoglou","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/10\/Byron-Kiourtzoglou-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/10\/Byron-Kiourtzoglou-96x96.jpg","caption":"Byron Kiourtzoglou"},"description":"Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications\/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.","sameAs":["https:\/\/www.pivotalgamers.com\/","https:\/\/www.linkedin.com\/in\/byron-kiourtzoglou-530ab222"],"url":"https:\/\/examples.javacodegeeks.com\/author\/byron-kiourtzoglou\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}