I have some XML data inside an XML file which i want to pass to another application. I have used XMLPullPaser.
<?xml version="1.0" encoding="utf-8" ?>
<node1>
<node2>dd03</node2>
<node3>,17,0,,**<xml><cell>555</cell></xml>**,</node3>
</bintextobj>
node 3 contains the data as highlighted. The xml I want to pass as data of xml file. Is there any way of achieving this?
Use CDATA to store the value. This will cause the parser to treat the value of node3 like plain text.
<node3><![CDATA[,17,0,,**<xml><cell>555</cell></xml>**,]]></node3>
write xsl to extract the <xml>..</xml> and write into the new xml file which can be passed to the other applications.
Related
I'm working with Jackson XML to covert JSON data into formatted xml output. So far, I have been able to convert the JSON data to java objects, which then are parsed to an XML output.
I'm trying to add a separate line to this xml (similar to adding ) but not sure how. Hoping the output for the xml comes out like this:
<?xml version="1.0" encoding="UTF-8"?>
<!--DOCTYPE ... -->
Is there a way to add this tag before my actual generated xml tags?
i have a XML file in : /res/xml/countries.xml
and also i have a view with editText inside.
i want to search that text (users should type in editText) in my countries.xml file!
here is my xml file :
<?xml version="1.0" encoding="utf-8"?>
<countries>
<country>
<name>United State</name>
</country>
<countries>
If you want to read a whole document, use XML Parser, like Pull already mentioned. If you only want to pick out only one or few certain things, I would suggest XPath. Here is a good Tutorial on how to use XPath in Java.
First you need to parse that xml file containing the countries name. Store list of countries in any arraylist and then write any search algo to search the text found from that edittext.
Try this xpath tutorial...
You will have good learning experience...
This tutorial explain clearly how to parse an XML file : XML parser
Other tutorial simpler with XPath : Xparse tutorial
I have a XML file, which is named input-kunde-2.xml.
from("file:c:/test/?fileName=input-kunde-2.xml")
<?xml version='1.0' encoding='UTF-8'?>
<list>
<Name>Ying</Name>
<Age>23</Age>
</list>
And i have used Apache Camel route this file from C:/ into another place D:/.
to("file:d:/test/?fileName=output-kunde-2.xml")
What do I want?
Now, i want change the content of XML file.
In the file input-kunde-2.xml: <Name>Ying</Name>
In the file output-kunde-2.xml: <Name>Wang</Name>
How can I implement it?
Ich have try to use:
File body = exchange.getIn().getBody(File.class);
//KundeDTO kundeDTO = exchange.getIn().getBody(KundeDTO.class);
But i could only get the path of the XML file.
Thanks a lot!
If you are wanting to extract values from the file, treat it as an XML document and make use of xpath
For instance with
.setHeader("myHeader").xpath("/list/Name/text()", String.class)
This documentation is here http://camel.apache.org/xpath.html
If you want to modify an entire XML document, then make use of XSLT. Consume your file with 'from', then pass it through the XSLT component, then send it to your file location
This documentation is here http://camel.apache.org/xslt.html
I use DOM parser to read data from XML file. I know how to read, modify and write back the data. However, I would like to know if it is possible to create an object from an XML file.
I have an XML file which looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE people SYSTEM "validator.dtd">
<people>
<student>
<name>John</name>
<course>Computer Technology</course>
<semester>6</semester>
<scheme>E</scheme>
</student>
<student>
<name>Foo</name>
<course>Industrial Electronics</course>
<semester>6</semester>
<scheme>E</scheme>
</student>
</people>
and I would like to make it an objects out of it so I can pass them around. Does a solution exist ?
Yes. This is possible through JAXB (Java API for XML binding)
All JAXB implementations provide a tool called a binding compiler to bind a XML schema in order to generate the corresponding Java classes.
For details refer: http://www.oracle.com/technetwork/articles/javase/index-140168.html#xmp1
You could have a look at XML beans or JAXB libraries. In case you don't have a schema file but have a sample XML file, you could create one using inst2xsd tool of xmlbeans. http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html. This could get you started with the schema.
can you help me in parsing xml with nested <?xml version="1.0" encoding="utf-8"?> tags. when i am trying to parse this xml, i m getting parsing error.
<?xml version="1.0" encoding="utf-8"?>
<soap>
<soapenvBody>
<serviceResponse>
<?xml version="1.0" encoding="UTF-8"?>
<data>
<respCode>0</respCode>
</data>
</serviceResponse>
</soapenvBody>
</soap>
I don't think this is really a Java problem. Having a second XML declaration within the XML body is just illegal, so I don't think you'll be able to get any XML parsers to parse that. If you have control over the XML (it looks like you're generating it to store a response) then you could try wrapping the inner-XML document with CDATA:
<?xml version="1.0" encoding="utf-8"?>
<soap>
<soapenvBody>
<serviceResponse>
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<data>
<respCode>0</respCode>
</data>
]]>
</serviceResponse>
</soapenvBody>
</soap>
EDIT:
I'm thinking that you most likely don't want the extra XML declaration inside that response at all. Do you have control over the code that creates the response? My guess is that the XML snippet <data>...</data> is created as a separate DOM object and then the string is spliced in the middle of the response. Writing out the entire XML document object results in the XML declaration being included, but if you just grab the document root node object (<data>) and write that out as a string then it probably won't include the extra XML declaration that's causing you all this trouble.
It occurred to me that a parser made for dealing with HTML might be able to do what you want. Since HTML tends to be a total mess compared to strict XML, HTML parsers are usually much more error-tolerant. A quick search turned up jsoup. I was able to pull the respCode from your sample XML above with roughly this code:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
String data = "your xml goes here";
Document doc = Jsoup.parse(data);
String respCodeRaw = doc.select("respCode").first().text();
int respCode = Integer.valueOf(respCodeRaw);
(I actually tested the library in the Clojure repl, but the code above should work!)
A tag that starts with like <? is a processing instruction. <?xml...> is an XML declaration, and can only be present at the beginning of the xml content. It's not allowed in the XML body.
Why does your soap body contain this? Do you have the option of removing it?
i did not find any parser in java to parse such embedded xml as it is not a valid xml and i guess almost all parses validate the xml before parsing it. so i choose the option to preprocess the xml and selected the inner xml then using SAX parser i parsed the xml and retrieved the values from xml. Guys thanks for your replies.