Issue
I need a simple Java code modification to import only the text content of an XML element instead of the entire element.
How can I get only the value of the tag name using getElementsByTagName()
?
This is the XML file I am reading named A (1).xml:
<?xml version="1.0" encoding="UTF-8"?>
<site
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->
<map>I want to import this content only</map>
</site>
Note that the file contains a <map>
element one line from the end.
This is the content of the XML file I am writing to:
<?xml version="1.0" encoding="UTF-8"?>
<tap
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->
<site>I do not want to lose this content </site>
</tap>
I want to extract the text value of TagName("map"), but not the tags themselves, and write that text to file shown above. This is my code:
package startimes;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import java.io.*;
public class Startimes {
public static void main(String argv[]) {
int r = 1;
int l = 1;
int s = 1;
String nom;
for (int i = 0; i < s; i++) {
nom = "B (" + (i + 1);
t(r, r + l, nom);
r = r + l;
}
}
public static void t(int f, int g, String z) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
Document doc = null;
Document doc2 = null;
String a = "C:\\Users\\chirap\\Desktop\\Startimes\\C.xml";
String c;
try {
db = dbf.newDocumentBuilder();
doc = db.parse(new File(a));
doc2 = db.parse(new File("C:\\Users\\chirap\\Desktop\\Startimes\\A (1).xml"));
NodeList ndListFirstFile = doc.getElementsByTagName("site");
Node nodeArea = doc.importNode(doc2.getElementsByTagName("map").item(0), true);
NodeList nList2 = doc2.getElementsByTagName("map");
for (int i = f; i < g; i++) {
c = i + "";
doc2 = db.parse(new File("C:\\Users\\chirap\\Desktop\\Startimes\\A (" + c + ").xml"));
for (int temp = 0; temp < nList2.getLength(); temp++) {
nodeArea = doc.importNode(doc2.getElementsByTagName("map").item(temp), true);
ndListFirstFile.item(0).appendChild(nodeArea);
}
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);
Writer output = new BufferedWriter(new FileWriter("C:\\Users\\chirap\\Desktop\\Startimes\\" + z + ").xml"));
String xmlOutput = result.getWriter().toString();
output.write(xmlOutput);
output.close();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
I want this result:
<?xml version="1.0" encoding="UTF-8"?>
<tap
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->
<site>I do not want to lose this content
I want to import this content only</site>
</tap>
Note that the last line but one should only contain text, but when I run my code it looks like this:
<map>I want to import this content only</map>
I want to exclude those <map>
tags. Thank you in advance
Solution
try with this,
for (int temp = 0; temp < nList2.getLength(); temp++) {
ndListFirstFile.item(0).setTextContent(ndListFirstFile.item(0).getTextContent()+nodeArea.getTextContent());
}
Answered By - Lakshan
Answer Checked By - Mildred Charles (JavaFixing Admin)