Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7719867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:31:21+00:00 2026-06-01T03:31:21+00:00

How can i extract attribute value out of the element. My xml node is

  • 0

How can i extract attribute value out of the element. My xml node is writen like this
< nodename attribute=”value” > i need to extract it out to compare it against another string.

But since i am not calling document.getElementsByTag then i cant use .getAttribute(“att.”).getNodeValue to get the value.

Instead i have a NodeList and getAttribute() does not have getNodeValue.

package dev;

import java.io.*;
import java.util.*;

import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;



public class Parser {


    static String def = "\"admin\",\"base\",\"Default\",\"simple\"";

    static String category = "";
    static String sku = "";
    static String has_options = "0";

    static String name = "";
    static String image = "";
    static String small_image = "";
    static String thumbnail = "";

    public static void toCSV() {
        try {
            BufferedWriter output = new BufferedWriter(new FileWriter("sim.csv", true));
            output.newLine();
            output.write(def);
            output.write(String.format(",\"%s\",\"%s\",\"%s\"", category, sku, has_options));
            output.write(String.format(",\"%s\",\"%s\",\"%s\",\"%s\"", name, image, small_image, thumbnail));
            output.flush();
            output.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        toCSV();
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();

            Document document = builder.parse(new File("input.asp.xml"));
            document.getDocumentElement().normalize();

            NodeList list = document.getElementsByTagName("izdelek");
            for(int i = 0; i < 1; i++) {
                NodeList child = list.item(i).getChildNodes();
                for(int j = 0; j < child.getLength(); j++) {
                    if(child.item(j).getNodeName().equals("kategorija")) {
                        category = child.item(j).getTextContent().trim();
                    } else if(child.item(j).getNodeName().equals("ean")) {
                        sku = child.item(j).getTextContent().trim();
                    } else if(child.item(j).getNodeName().equals("izdelekIme")) {
                        name = child.item(j).getTextContent().trim();
                    } else if(child.item(j).getNodeName().equals("slikaMala")) {
                        small_image = child.item(j).getTextContent().trim();
                        thumbnail   = child.item(j).getTextContent().trim();
                    } else if(child.item(j).getNodeName().equals("slikaVelika")) {
                        image = child.item(j).getTextContent().trim();
                    } else if(child.item(j).getNodeName().equals("dodatneLastnosti")) {
                        NodeList subs = child.item(j).getChildNodes();
                        // ^ need to parse these nodes they are written as <nodename attribute="value">
                        // i need to print out the value
                    } 
                }
                //toCSV();
            }

        } catch(Exception io) {
            io.printStackTrace();
        } 
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:31:22+00:00Added an answer on June 1, 2026 at 3:31 am

    Solved:

    XML input:

    < nodename attribute="value" > Something </ nodename>
    

    Java code:

    NodesList subs = child.item(j).getChildNodes();
    
    System.out.println(subs.item(0).getTextContent()); // >> Something
    
    Element element = (Element) document.adoptNode(subs.item(0));
    System.out.println(element.getAttribute("attribute"));  // >> value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I extract the value of an attribute node via XPath? A sample
Lets consider this xml file : <?xml version=1.0 encoding=UTF-8?> <root attribute=value> <element>myElement</element> </root> I'm
I want to extract part of value of element and assign that as attribute
I am experimenting with video and would like to know how I can extract
I can't quite get my query correct. Given this XML: <?xml version=1.0 encoding=utf-8 ?>
I am stumped as to how I can extract the href attribute from the
I have an XML file with this structure: <?xml version=1.0> <person> <element att1=value1 att2=value2>Anonymous</element>
I have a SQL query like this : SELECT myXmlField.value('(./row/@myAttribute)[1]', 'nVarchar(max)') as myAttribute FROM
I have an XML file, which I open in F# like this: let Bookmarks(xmlFile:string)
How can I do a Linq query for this? I have two xml documents,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.