How to build HierarchicalConfiguration object from xml string content? - java

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

Related

Can I parse XML in Java without taking XML file input from outside?

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.

Java replacing ampersand in XML file

I am processing a list of URLS containing XML files. My problem is that some of them are not well formed because they contain "&"(ampersand) characters,l so my code cannot parse it correctly.
<elementType>CK037 - AT&ZN -SET</elementType>
How could I avoid this?? Should I first read the XML as a String and replace the "&" with "amp;" ?? Are there any other more appropiate solutions for my problem??
This is my code:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document doc = null;
try {
doc = factory.newDocumentBuilder().parse(new URL(inputURLString).openStream());
(...)
Thanks in advance.

Http charset vs xml encoding (utf-8, utf-16, etc)

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.

Problems reading XML InputStream in Java

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()

how to parse XML document?

I have xml document in variable (not in file). How can i get data storaged in that? I don't have any additional file with that, i have it 'inside' my sourcecode. When i use
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(XML);
(XML is my xml variable), i get an error
java.io.FileNotFoundException: C:\netbeans\app-s7013\<network ip_addr="10.0.0.0\8" save_ip="true"> File not found.
Read your XML into a StringReader, wrap it in an InputSource, and pass that to your DocumentBuilder:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));
Assuming that XML is a String, don't be confused by the version that takes a string - the string is a URL, not your input!
What you need is the version that takes an input stream.
You need to create an input stream based on a string (I'll try and find code sample, but you can Google for that). Usually a StringReader is involved.

Categories

Resources