I have this XML code:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="https://www.cvlkra.com/">tTKyEndh0iBqnZdjpUntEQ%3d%3d</string>
I want to get this: tTKyEndh0iBqnZdjpUntEQ%3d%3d for which I have tried the below code:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder1 = factory.newDocumentBuilder();
Document document = builder1.parse(new InputSource(new StringReader(string)));
Element rootElement = document.getDocumentElement();
String nodeName = rootElement.getNodeName();
But i am not getting it. I am getting null value instead of tTKyEndh0iBqnZdjpUntEQ%3d%3d even when I have tried some other code also.
Try using getTextContent() instead getNodeValue() returns null because it has no values.
You should not use getNodeName() instead use rootElement.getNodeValue(). May be this helps.
Related
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();.
I am using following code to read XML from webpage. I have mentioned public URL here as cant mention project URL:
`String g1="http://www.w3schools.com/xml/note.xml";
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(g1);`
but I am receiving value of doc as null.
Do something like :-
String urlString = "http://www.w3schools.com/xml/note.xml";
URL url = new URL(urlString);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(url.openStream());
NodeList descNodes = doc.getElementsByTagName("note");
for(int i=0; i<descNodes.getLength();i++)
{
System.out.println(descNodes.item(i).getTextContent());
}
Output:-
Tove
Jani
Reminder
Don't forget me this weekend!
Refer below link hope it helps you..see the part for 1.2 in it !!
http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/
I am using this function :
public void testing(String xml) throws ParserConfigurationException, SAXException, IOException{
Log.d("TAG"," root.getNodeName()");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xml);
//document.getDocumentElement().normalize();
//Element root = document.getDocumentElement();
//Log.d("TAG", root.getNodeName());
Log.d("TAG"," root.getNodeName()");
}
And I am calling this function like this :
testing(responseText)
Where response text is this:
<?xml version='1.0' encoding='UTF-8'?>
<queryresult success='true'
error='false'
numpods='2'
datatypes=''
timedout=''
timedoutpods=''
timing='0.751'
parsetiming='0.216'
parsetimedout='false'
recalculate='http://www4b.wolframalpha.com/api/v2/recalc.jsp?id=MSPa2715236aaf6db55age00000025hbhc18c61h80c4&s=10'
id='MSPa2716236aaf6db55age00000019f566b957ic219h'
host='http://www4b.wolframalpha.com'
server='10'
related='http://www4b.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa2717236aaf6db55age000000535a701459c5c90a&s=10'
version='2.6'>
<pod title='Input interpretation'
scanner='Identity'
id='Input'
position='100'
error='false'
numsubpods='1'>
<subpod title=''>
<plaintext>Tell me a joke.</plaintext>
</subpod>
</pod>
<pod title='Result'
scanner='Data'
id='Result'
position='200'
But im getting the error:
04-06 22:19:14.348: D/TAG(30413): java.net.MalformedURLException:
Protocol not found:
What am I doing wrong ?
Note that I am getting this responseText from a server. So if theres any problem with the xml itself, do tell me how to manipulate the string, instead of suggesting me to change the xml itself.
The problem is that you're passing in the XML content itself - but DocumentBuilder.parse(String) accepts a URL to load the XML from - not the content itself.
You probably want to use DocumentBuilder.parse(InputSource) instead, having created an InputSource from a StringReader wrapping the XML:
Document document = builder.parse(new InputSource(new StringReader(xml)));
I am using XMLConfiguration to get DOM Document object from the configuration object as
given below:
XMLConfiguration config = new XMLConfiguration("xml file path");
Document document = config.getDocument();
But it is returning null document object .
Am I using right approach?
If you didn't get an exception thrown by new XMLConfiguration(), it means that configuration was loaded successfully.
I'm willing to bet that you concluded that it's a "null document object" because you tried to print the value of document and got something like:
[Document: null]
That doesn't mean a "null document".
You can use below approach also:
File xmlFile = new File("xml file path");
DocumentBuilderFactory documentFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentFactory
.newDocumentBuilder();
Document doc = documentBuilder.parse(xmlFile);
I have a series of XML files I am looking through and grabbing a specific element from.
<key>A</key>
I'm using this snippet of code to grab the XML element, but it returns null instead of the element I am looking for. I am not able to change the XML files.
File key = new File(filePath);
PrintWriter keyWriter = new PrintWriter(key);
File xmlFile = new File(configPath);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(xmlFile);
NodeList nodes = document.getElementsByTagName("key");
Element keyValue = (Element) nodes.item(0);
keyWriter.println(keyValue);
keyWriter.close();
}
I've tried using the document method as well as the apache xmlconfiguration and getElementbyId but all have returned null so far.
I noticed in your code that your passing the element object to the writer's println function as in:
keyWriter.println(keyValue);
This will print a null value in the file. Try replacing it with:
keyWriter.println(keyValue.getTextContent());