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?
Related
I have to marshal some class in XML format to the file (with no xml extension) as a header, and then append at the end of a file some invalid content. After that, obviously I would like to unmarshal this class with a JAXB unmarshaller, but I got a problem - it returns null, because the file is no longer valid xml (without additional content, all works perfect).
My file looks like:
<Root>
<Element>
...
</Element>
...
</Root>
[binary invalid content]
Is that possible to unmarshal only xml valid part of a file?
Thanks for all your help.
i am having a xml something looks like this
<?xml version='1.0' encoding='utf-8'?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><content></content>
i just want to extract the content alone,i am expecting something like this.
<content></content>
i know String replace is a solution but it changes every time.
Thanks.
Using jsoup:
String extracted = Jsoup.parse(xml).getElementsByTag("s:body").first().html();
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.
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.