hello
I have a xml like this:

    <book>
            <Publication> myPublication0</Publication>
            <Publication> myPublication<bookName> my book</bookName></Publication>
            <Publication> myPublication1</Publication>
            <Publication> myPublication2<bookName> my book2</bookName>asd</Publication>
    </book>

and i want to read through java program and want output like

myPublication0
myPublication<bookName> my book</bookName>
myPublication1
myPublication2<bookName> my book2</bookName>asd

please tell how to get given output

Dani AI

Generated

Short answer for : JDOM is a third‑party library (not bundled in the JDK), as already said. If you want to avoid adding an external jar you can use the JDK DOM APIs and serialize each Publication element's child nodes — that preserves text plus any nested element tags (for example bookName) exactly as inner XML.

Here is a minimal JDK-only example that reads an XML file and prints the inner XML of every Publication node (text nodes and nested elements are preserved):

import java.io.File;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ExtractPublicationInnerXml {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new File("book.xml"));

        NodeList pubs = doc.getElementsByTagName("Publication");
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "no");

        for (int i = 0; i < pubs.getLength(); i++) {
            Node pub = pubs.item(i);
            StringWriter sw = new StringWriter();
            NodeList children = pub.getChildNodes();
            for (int c = 0; c < children.getLength(); c++) {
                t.transform(new DOMSource(children.item(c)), new StreamResult(sw));
            }
            System.out.println(sw.toString().trim());
        }
    }
}

Notes and gotchas:

  • If your input actually contains escaped markup (for example the angle brackets are already escaped as text) the parser sees them as text and you will get entities like &lt;bookName&gt;. To turn those into element nodes you must unescape and parse them as XML again.
  • Whitespace: DOM will preserve text nodes exactly. If you see extra indentation/newlines, trim or normalize text nodes or adjust parsing/serialization settings.
  • If you prefer a friendlier API, you can use libraries such as JDOM/XOM or a streaming parser — but they are external and must be added to your project (Maven, Gradle, or jar). This JDK approach keeps your project dependency-free and directly produces the inner XML you described.

Recommended Answers

All 3 Replies

read the input
process the input to output
look at the output.

if you need to work with xml using Java, JDom is a stable and very well documented library that can help you there.

is JDOM is already present in JDK???

nope. all you need can be found at the link I gave you earlier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.