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 9169473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:53:03+00:00 2026-06-17T15:53:03+00:00

I have xml documents that look like this: <?xml version=1.0?> <root> <success>true</success> <note> <note_id>32219</note_id>

  • 0

I have xml documents that look like this:

<?xml version="1.0"?>
<root>
    <success>true</success>
    <note>
        <note_id>32219</note_id>
        <the_date>1336763490</the_date>
        <member_id>108649</member_id>
        <area>6</area>
        <note>Note 123123123</note>
    </note>
    <note>
        <note_id>33734</note_id>
        <the_date>1339003652</the_date>
        <member_id>108649</member_id>
        <area>1</area>
        <note>This is another note.</note>
    </note>
    <note>
        <note_id>49617</note_id>
        <the_date>1343050791</the_date>
        <member_id>108649</member_id>
        <area>1</area>
        <note>this is a 3rd note.</note>
    </note>
</root>

I would like to take that document, and get all of the <note> tags and convert them to a string, then pass them to my XML class and place the XML class into an array list. I hope that makes sense. So Here is the method that I am trying to use to get all of the <note> tags.

public ArrayList<XML> getNodes(String root, String name){
    ArrayList<XML> elList = new ArrayList<>();
    NodeList nodes = doc.getElementsByTagName(root);
    for(int i = 0; i < nodes.getLength(); i++){
        Element element = (Element)nodes.item(i);
        NodeList nl = element.getElementsByTagName(name);
        for(int c = 0; c < nl.getLength(); c++){
            Element e = (Element)nl.item(c);
            String xmlStr = this.nodeToString(e);
            XML xml = new XML();
            xml.parse(xmlStr);
            elList.add(xml);
        }
    }
    return elList;
}

private String nodeToString(Node node){
    StringWriter sw = new StringWriter();
    try{
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    }catch(TransformerException te){
        System.out.println("nodeToString Transformer Exception");
    }
    return sw.toString();
}

So, my question is, how can I get each <note> tag as a string? With the code I have now all I get back is null for String xmlStr = e.getNodeValue();.

Edit
I edited my main code, this seems to work.

  • 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-17T15:53:03+00:00Added an answer on June 17, 2026 at 3:53 pm

    Updated after clarification

    You can find all the <note> elements using XPath.

    This will allow you to isolate each node simply. You can then create a new document, based on the found nodes and transform it back to string

    public class TestXML01 {
    
        public static void main(String[] args) {
    
            String xml = "<?xml version=\"1.0\"?>";
            xml += "<root>";
            xml += "<success>true</success>";
            xml += "<note>";
            xml += "<note_id>32219</note_id>";
            xml += "<the_date>1336763490</the_date>";
            xml += "<member_id>108649</member_id>";
            xml += "<area>6</area>";
            xml += "<note>Note 123123123</note>";
            xml += "</note>";
            xml += "<note>";
            xml += "<note_id>33734</note_id>";
            xml += "<the_date>1339003652</the_date>";
            xml += "<member_id>108649</member_id>";
            xml += "<area>1</area>";
            xml += "<note>This is another note.</note>";
            xml += "</note>";
            xml += "<note>";
            xml += "<note_id>49617</note_id>";
            xml += "<the_date>1343050791</the_date>";
            xml += "<member_id>108649</member_id>";
            xml += "<area>1</area>";
            xml += "<note>this is a 3rd note.</note>";
            xml += "</note>";
            xml += "</root>";
    
            ByteArrayInputStream bais = null;
    
            try {
                bais = new ByteArrayInputStream(xml.getBytes());
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                factory.setNamespaceAware(false);
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document xmlDoc = builder.parse(bais);
    
                Node root = xmlDoc.getDocumentElement();
    
                XPathFactory xFactory = XPathFactory.newInstance();
                XPath xPath = xFactory.newXPath();
    
                XPathExpression xExpress = xPath.compile("/root/note");
                NodeList nodes = (NodeList) xExpress.evaluate(root, XPathConstants.NODESET);
    
                System.out.println("Found " + nodes.getLength() + " note nodes");
    
                for (int index = 0; index < nodes.getLength(); index++) {
                    Node node = nodes.item(index);
                    Document childDoc = builder.newDocument();
                    childDoc.adoptNode(node);
                    childDoc.appendChild(node);
                    System.out.println(toString(childDoc));
                }
    
            } catch (Exception exp) {
                exp.printStackTrace();
            } finally {
                try {
                    bais.close();
                } catch (Exception e) {
                }
            }
        }
    
        public static String toString(Document doc) {
    
            String sValue = null;
    
            ByteArrayOutputStream baos = null;
            OutputStreamWriter osw = null;
    
            try {
                baos = new ByteArrayOutputStream();
                osw = new OutputStreamWriter(baos);
    
                Transformer tf = TransformerFactory.newInstance().newTransformer();
                tf.setOutputProperty(OutputKeys.INDENT, "yes");
                tf.setOutputProperty(OutputKeys.METHOD, "xml");
                tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    
                DOMSource domSource = new DOMSource(doc);
                StreamResult sr = new StreamResult(osw);
                tf.transform(domSource, sr);
    
                osw.flush();
                baos.flush();
                sValue = new String(baos.toByteArray());
            } catch (Exception exp) {
                exp.printStackTrace();
            } finally {
                try {
                    osw.close();
                } catch (Exception exp) {
                }
                try {
                    baos.close();
                } catch (Exception exp) {
                }
            }
            return sValue;
        }
    }
    

    This now outputs…

    Found 3 note nodes
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <note>
        <note_id>32219</note_id>
        <the_date>1336763490</the_date>
        <member_id>108649</member_id>
        <area>6</area>
        <note>Note 123123123</note>
    </note>
    
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <note>
        <note_id>33734</note_id>
        <the_date>1339003652</the_date>
        <member_id>108649</member_id>
        <area>1</area>
        <note>This is another note.</note>
    </note>
    
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <note>
        <note_id>49617</note_id>
        <the_date>1343050791</the_date>
        <member_id>108649</member_id>
        <area>1</area>
        <note>this is a 3rd note.</note>
    </note>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple xml file that looks like this: <?xml version=1.0 encoding=UTF-8 standalone=yes
I have an xml document that looks something like this <?xml version=1.0 encoding=UTF-8?> <mapPoints>
I have an xml document that looks like this. <foo> <bar type=artist/> Bob Marley
I have an XML document that matches our site navigation something like this: <page
I have a file that consists of concatenated valid XML documents. I'd like to
I have an XML document that looks like this: <file> <name>NAME_OF_FILE</name> </file> <file> <name>NAME_OF_FILE</name>
I have several XDocuments that look like: <Test> <element location=.\jnk.txt status=(modified)/> <element location=.\jnk.xml status=(overload)/>
I have a XML code like this: <?xml version=1.0 encoding=utf-8 ?> <Window xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml>
I have a bunch of legacy documents that are HTML-like. As in, they look
I have a piece of XML that is structured similar to this: <root> <score

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.