In my main activity I have this call:
InputStream stream = http_conn.getInputStream();
ParseXML.Login(stream);
I know the input stream is working as I can create a buffered reader, creating a string that I can send to the UI. The issue is this reports the entire XML document that is being returned to me.
Within my ParseXML class Login method, I have the following:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(stream);
doc.getDocumentElement().normalize();
So far so good, I think? I am new to using parsers, but basically the layout of my XML document is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://www.xxx.com/asmx">TOKEN HERE</string>
I have seen examples in which you can retrieve various items from deeper with an XML file, as per the example here: http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
I'm not only new to XML parsers but new to java as well, I just can't figure out how to pull that string out of the XML document!
Thanks
I don't know if I'm understanding but if you want to get only TOKEN HERE try doc.getDocumentElement().getTextContent()
Related
I have xml String content which is passed as part of request body in rest API,
<?xml version="1.0" encoding="UTF-8"?>
<sdp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="repository://schemas/sdp-config.xsd">
<helpPage>/mstinc/sdp/help/index.html</helpPage>
<ib>
<siteUrl>/onlineserv/HB/</siteUrl>
<startUpPage>
<sdpUrl>SECONDARY~PRIMARY_BUTTON_ACCOUNT_ACCESS.NAME~ACCOUNT_ACCESS_SECONDARY_BUTTON_SDP.NAME</sdpUrl>
<otherUrl>SECONDARY~PRIMARY_BUTTON_ACCOUNT_ACCESS.NAME~ACCOUNT_ACCESS_SECONDARY_BUTTON_ACCOUNT_SUMMARY.NAME</otherUrl>
<axisConfValue>true</axisConfValue>
</startUpPage>
</ib>
I would like to build HierarchicalConfiguration object so that i can iterate through keys using,
Iterator keys = {hierachicalObject}.getKeys();
I don't want to create a file as the content is passed dynamically for each request. How can i do it?
I think you can get information from the tag in String and work with this string.
For example :
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
String keys = doc.getElementsByTagName("sdpUrl").item(0).getTextContent();
And after that working with keys. But there used file.
There you can read how to get data from String XML : Read a XML (from a string) and get some fields - Problems reading XML
Generally using DOM, SAX or XPath etc parser we do take input from outside Java code like this:
File inputFile = new File("C:\\Users\\DELL\\Desktop\\catalog.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
So can you parse XML file without taking input like this? I want to write my XML code alongside Java code.
Use DocumentBuilder.parse(new InputStream(new StringReader(xml))) where xml is a string containing the XML to be parsed.
That's if you really must use DOM. I can't imagine why anyone uses it any more, when alternatives such as JDOM2 are so much better.
Which one I should use to parse the xml file. what is the recommended approach to the parse http-xml file. my approach is read xml as String and use DocumentBuilder to parse the String.
Is this right approach.
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
Document doc = null;
InputSource is = null;
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
is = new InputSource(new StringReader(xmlString));
doc = dBuilder.parse(is);
XML specifies its own encoding in <!xml encoding="..."> defaulting to UTF-8.
Using a StringReader using a String, already assumes that the reading has been done in a guessed encoding. That seems less recommendable, than using a pure binary format, like File or InputStream.
Another factor is the document base, to find included documents, xsd, dtd. There the usage of an XML catalog might help, storing such files offline.
I'm currently converting a program I wrote in Visual Basic .NET (the 2005 variety) into Java. It used built-in XML methods to parse and generate the user's saved data, does Java have an equivalent feature built in or am I going to have to change file processing implementations? (I'd rather not, there's a lot of code I'd have to change.)
Yes, Java can parse XML. Here's an example that takes in a String that contains XML and builds a Document object out of it:
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(xml));
Document document = documentBuilder.parse(inputSource);
You can then use the XPath API to query the dom. Here's a tutorial/writeup about it.
As far as serializing objects to XML, the official implementation is JAXB and it is part of Java since 1.6. Here's a simple example. It will let you serialize and deserialize to and from XML.
You can also create a DOM object manually and add nodes to it, but it's a little more tedious:
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootNode = document.createElement("root");
Element childNode = document.createElement("child");
childNode.setTextContent("I am a child node");
childNode.setAttribute("attr", "value");
rootNode.appendChild(childNode);
document.appendChild(rootNode);
I'm assuming that you mean that the properties/structure was generated through the classes/beans themselves? If so, then the answer is no [without an third party component]. I've used XStream before, and that is about the closest that I've gotten to .NET's XML Class serialization.
I have this code:
if (file.exists()) {
Document doc = builder.parse(file);
NodeList list = doc.getElementsByTagName("property");
System.out.println("XML Elements: ");
for (int ii = 0; ii < list.getLength(); ii++) {
line 2 gives following exception
E:\workspace\test\testDomain\src\com\test\ins\nxg\maps\Right.hbm.xml
...***java.net.SocketException: Operation timed out: connect:could be due to invalid address
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:372)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:233)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:220)
Parhaps the DocumentBuilder is unsuccessfully trying to access a DTD via a network socket for your XML document?
If there are DTD references in the XML document, try editing them out to prove the cause.
If that fixes your problem, I think you can use an EntityResolver for a more permanent solution, but I've not done it myself.
The answer by Brabster is very helpful to me. In my case I have an XML document starting with
<?xml version="1.0"?> <!DOCTYPE GBSet PUBLIC "-//NCBI//NCBI GBSeq/EN" http://www.ncbi.nlm.nih.gov/dtd/NCBI_GBSeq.dtd"> ... more to come
This caused a problem for DocumentBuilder. I got a time out problem. The true evil is in the content of the URL: http://www.ncbi.nlm.nih.gov/dtd/NCBI_GBSeq.dtd:
<!-- ============================================
::DATATOOL:: Generated from "gbseq.asn"
::DATATOOL:: by application DATATOOL version 1.5.0
::DATATOOL:: on 06/06/2006 23:03:48
============================================ -->
<!-- NCBI_GBSeq.dtd
This file is built from a series of basic modules.
The actual ELEMENT and ENTITY declarations are in the modules.
This file is used to put them together.
-->
<!ENTITY % NCBI_Entity_module PUBLIC "-//NCBI//NCBI Entity Module//EN"
"NCBI_Entity.mod.dtd"> %NCBI_Entity_module;
<!ENTITY % NCBI_GBSeq_module PUBLIC "-//NCBI//NCBI GBSeq Module//EN" "NCBI_GBSeq.mod.dtd"> %NCBI_GBSeq_module;
After deleting
<!DOCTYPE GBSet PUBLIC "-//NCBI//NCBI GBSeq/EN" "http://www.ncbi.nlm.nih.gov/dtd/NCBI_GBSeq.dtd">
My program can at least move forward!
Try to simplify your problem.
Can you get the code, you have to parse, manually?
If yes, try to parse it. I don't think it's the problem of your DocumentBuilder but your network connection. So you have to ensure, that the DocumentBuilder is able to access every bit of the xml document.
If your manually stored document fails when it is validated, there will be a different error message.
Hope it helps.
Did you create a new instance of a DocumentBuilderFactory and then create a newDocumentBuilder before you parse the file?
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
Hope this link helps. It definitely helped me earlier today.