How to read comments from XML file using DOM parser - java

I'm trying to read the comments in an XML file. Not able to get the comment nodes after parsing it with DOM.
Code:
DocumentBuilderFactory docBldrFactry = DocumentBuilderFactory.newInstance();
docBldrFactry.setIgnoringComments(false);
DocumentBuilder docBuilder = docBldrFactry.newDocumentBuilder();
Document document = docBuilder.parse(new File("C:\\webser.xml"));
Element rootElement = document.getDocumentElement();
NodeList list = rootElement.getElementsByTagName("Bean");
Comments are under Bean element. WHen I loop through the child nodes of Bean element, I don't get the comment nodes.
Here is the XML
<Beans>
<Bean>
<!-- Testing Comments -->
<API name ="xyz" />
</Bean>
</Beans>

Hope this helps :)
XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream("file.xml"));
while (xr.hasNext()) {
if (xr.next() == XMLStreamConstants.COMMENT) {
String comment = xr.getText();
} }

Related

How to extract XML data using Java in Android?

I have the following XML document which I'm trying to get the inner text. I have tried numerous ways, using Xpath, DOM, SAX but no success.
This is my XML, I'm not sure if it's the XML structure which is causing a problem or my code.
<?xml version="1.0"?>
<ArrayOfPurchaseEntitites xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema">
<PurchaseEntitites>
<rInstalmentAmt>634.0</rInstalmentAmt>
<rAnnualRate>12.0</rAnnualRate>
<rInterestAmt>2670.0</rInterestAmt>
<dFirstInstalment>3/31/2016 12:00:00 AM</dFirstInstalment>
<dLastInstalment>8/31/2018 12:00:00 AM</dLastInstalment>
<rInsurancePremium>1350.0</rInsurancePremium>
<sResponseCode>00</sResponseCode>
</PurchaseEntitites>
</ArrayOfPurchaseEntitites>
InputStream stream = connect.getInputStream();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(stream);
doc.normalize();
System.out.println("===============================================================");
String g = doc.getDocumentElement().getTextContent();
System.out.println(g);
NodeList rootNodes = doc.getElementsByTagName("ArrayOfPurchaseEntitites");
Node rootnode =rootNodes.item(0);
Element rootElement = (Element) rootnode;
NodeList noteslist = rootElement.getElementsByTagName("PurchaseEntitites");
for(int i = 0; i < noteslist.getLength(); i++)
{
Node theNote = noteslist.item(i);
Element noteElement =(Element) theNote;
Node theExpiryDate = noteElement.getElementsByTagName("dLastInstalment").item(0);
Element dateElement = (Element) theExpiryDate;
System.out.println(dateElement.getTextContent());
}
stream.close();
I had a similar problem where I wanted to call getElementsByTagName for the first item in a NodeList. The trick - which you already utilize - is to cast the Node to Element. However, just to be sure, I suggest you add if (rootnode instanceof Element).
Assuming you use packages javax.xml.parsers and org.w3c.dom (no wild guess) your code works nicely when the xml is read from a file.
So if there still a problem with the code (it's been a while since this question was asked) I suggest you update the question with more info regarding connect.getInputStream();.

Create Header Elements with Java Xerces XML Creation

How can I add standard header statements to XML documents that I generate using Java Xerces?
Something like this:?
<?xml version="1.0" encoding="utf-8"?>
<!--My Comment for this XML File -->
<?TSMKey applanguage="EN" appversion="4.3.0" dtdversion="1.6.2"?>
<!DOCTYPE KEYS SYSTEM "C:\TSM\System\DTD\TSMLte.dtd">
I'm getting the <?xml> tag by default now, but how can I add the other header elements?
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("TOP");
doc.appendChild(rootElement);
DOMSource domSource = new DOMSource(doc);
The creation of the processing instruction is IMHO not well supported, but you can do the following:
Comment comment = document.createComment("My Comment for this XML File");
document.appendChild(comment);
ProcessingInstruction processingInstruction = document.createProcessingInstruction("TSMKey", "applanguage=\"EN\" appversion=\"4.3.0\" dtdversion=\"1.6.2\"");
document.appendChild(processingInstruction);
Element rootElement = document.createElement("TOP");
document.appendChild(rootElement);

xml search element by id

Can we search a element by id in xml file using dom parser, for example :
<root>
<context id="one">
<entity>
<identifier>new one</identifier>
</entity>
</context>
<context id="two">
<entity>
<identifier>second one</identifier>
</entity>
</context>
</root>
I want a node with id = "one", my code
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File("filename.xml"));
Element ele = document.getElementById("one");
return null,
is there any other way?
From the documentation for Document.getElementById
Note: Attributes with the name "ID" or "id" are not of type ID unless so defined.
The problem is the Document doesn't know that an attribute called id is an identifier unless you tell it. You need to set a schema on the DocumentBuilderFactory before you call newDocumentBuilder. That way the DocumentBuilder will be aware of the element types.
In the schema you will need something like this in the appropriate place:
<xs:attribute name="id" type="xs:ID"/>
You could use the javax.xml.xpath APIs in the JDK/JRE to find the element by XPath.
Example
import java.io.File;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
public class Demo {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File("filename.xml"));
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
Element element = (Element) xpath.evaluate("//*[#id='one']", document, XPathConstants.NODE);
}
}
You could instead use a third party library like Jsoup that does the job rather quite well.
File input = new File("/tmp/input.xml");
Document doc = Jsoup.parse(input, "UTF-8", "test");
And then you could use something like this:
doc.select("context#id=one")
Does that answer your question?
try and use XML - Xpath expression it is very easy
File fXmlFile = new File("filePath");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
String expression;
Node node;
// 1. elements with id '1'
expression = "//context[#id='one']";
node = (Node ) xpath.evaluate(expression, doc, XPathConstants.NODE);

Load XML into TreeMap

I have an XML config file that has just one parent and one child. This will always be like this and never change. It looks something like this:
<parent>
<child1>test</child1>
<child2>123</child2>
</parent>
I want to use java DOM (org.w3c.dom.Document) to parse the XML into a TreeMap so that I can access the attributes as keys/values. I'm guessing I'd need to create a for loop that scans through the XML and adds the key (parent) and value (child) line by line?
You can traverse the XML document using JAXP APIs, you don't need to know the structure or node names in advance
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(is);
NodeList nodeList = doc.getChildNodes();
and you can iterate on document and get the nodes and attributes
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
NamedNodeMap attributes = node.getAttributes();
//...
}

Read a QName from an XML using Java / DOM

I have a QName encoded in an XML file as below. How do I read it in to a Java javax.xml.namespace.QName? Also is this a proper way to encode a QName in an XML
<messages>
<message>
<qname xmlns:msg-i="http://www.abc.com/message">msg-i:InformationMessage</qname>
</message>
</messages>
and Java code
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document messagesDoc = db.parse(file);
messagesDoc.getDocumentElement().normalize();
NodeList messages = messagesDoc.getDocumentElement().getElementsByTagName("message");
for (int i = 0; i < messages.getLength(); i++) {
//read the QName here
QName qname = ...;
}
Yes it's better way to parse the xml elements and it's tag values, use Node and Element interface to iterate between the child nodes to parse the xml element values .I hop it would help you ..

Categories