<?xml version="1.0" encoding="utf-8"?>
<processor>
<user_config>
<a>xxxxx</a>
</user_config>
</processor>
I want to check if the user_config exist in this xml config file, is there any method I can use in org.apache.common.XMLConfiguration?
I solved the problem with:
if(configuration.configurationsAt( "user_config" ).size() > 0 ) {
//it exists
}
I don't like this solution. If anybody knows a better solution -> Please share.
While I am not sure about the XMLConfiguration you could use the org.apache.xpath to check if the user_config exists or parse it to a DOM object from either a string, inputstream or file and check it that way.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse("");
boolean containsKey(String key) would solve the problem: https://commons.apache.org/proper/commons-configuration/javadocs/v1.10/apidocs/org/apache/commons/configuration/Configuration.html#containsKey(java.lang.String)
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
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.
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()
I have a string which has an xml inside. I want to write this to a Document in java so i can later retrieve the attributes within it and store them in other strings. Could someone provide me with an example of this please?
Some more details are necessary for a proper answer but you can start with the DocumentBuilder like this:
DocumentBuilder db = DocumentBuilderFactory.newDocumentBuilder()
Document doc = db.parse(yourstring);
You now have a Document from which you can access your XML attributes etc.
Check out http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html. Hopefully it will give you a good starting point.
http://www.java-samples.com/showtutorial.php?tutorialid=152
This is a good example of how to parse out some XML. This should be a good start for you.
Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(myXMLString.getBytes());
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.