How to parse the XML fragment with SimpleXML in Android?
<txtList>
message <bold>message</bold> message
</txtList>
In fact, I do not know how to retrieve the value of an element that contains another element in this way?
You have to write a class with proper fields. Or - and thats what i recommend - you write a class that contains the values and a Transformer which "parses" the XML.
However, Simple XML ist not a XML-Parser, it's a Serializer. So you may have a look at e.g. JSoup, a very good HTML / XML Parser.
Related
Let's say there were errors in an XML message:
Well-Formed
<Person><Name>Attila</Name><ID>001</ID><Age>45</Age></Person>
Not Well-Formed
<Pxxxon><Name>Attila</9327><ID>001</ID><Age>45</Age></Person>
Are there any Java libraries or code to format the non-well formed XML message to:
<Pxxxon>
<Name>Attila</9327>
<ID>001</ID>
<Age>45</Age>
</Person>
I understand that current Java libraries only format valid XML messages to this prettified format.
No, because what you list as "Invalid" is actually not well-formed.
Well-formed and valid are not the same thing.
Well-formed means that a textual object meets the W3C requirements for being XML.
Valid means that well-formed XML meets additional requirements given by a specified schema.
See Well-formed vs Valid XML for more details, but if data is not well-formed, it is not XML at all and no XML parser will be able to read it to reformat it.
You might then ask what about non-XML parsers? To which we would reply, if it's not XML, what format is it? For any parser to be able to read any data, the syntax of the data has to be defined. Simply saying that the data resembles XML insufficiently specifies the format, and that is why you'll not find a tool that can pretty-print the data sample you provided.
I have a XML file which was encoded using java.beans.XMLEncoder. I cannot use java.beans.XMLDecoder to decode it, as the class of encoded object is not present in my project. Is there a way to obtain values in xml without using java.beans.XMLDecoder, xmlDecoder.readObject() method ?
There are several APIs for parsing XML in Java which don't require you to deserialize an object. Once you have a DOM, XPath is a useful way to query its contents. You will need to know what you're looking for, though.
I read elements with CDATA sections from a rss-feed which I need to convert to valid xml. The content in the CDATA section is mostly valid xhtml, but some times characters like ampersand appear in attributes (url's).
I can use .replaceAll("&", "&") to solve this but thinking a bit forward it may be that other invalid characters show up in attributes or text.
The CMS to which I'm importing the element, won't accept CDATA sections without setting up another configuration for the content, so my question is: is there any simple way to escape the string, only for attributes and text?
I'm using the jdom library to manipulate the xml after the import.
Edit: I've checked out apache's StringEscapeUtils, but this is escaping the whole string. I need something that will only escape attribute values and text inside elements.
Apache Commons provides handy functions for this: StringEscapeUtils
When you use JDOM it will automatically correctly escape ay content that needs it. Is your CMS loaded with the output of JDOM, or are you using some other library to populate the CMS...?
In essence, if you have valid XML input, and you use JDOM (something from org.jdom2.output.*) to output the data, then you will always have good output.... so, what are you doing to have broken output?
Rolf
I am writing a parser for an xml file which will contains special characters, for example
<name>You & me ®</name>
The dom parser will, by default, parse this value to "You & me ®".
However what I want the string is
You & me ®
Is there any way I can do this?
Thanks
If you are using DOM for parsing, see the DocumentBuilderFactory.setExpandEntityReferences() method.
By default, this setting is true meaning that entities are expanded out automatically. If you turn this off, you will be able to read the entities from the DOM - in this case you won't just get one big text node from a parent element, but you will get text nodes interleaved with entity nodes.
Is there any way to use an xsd file to validate input of a string?
I have found some examples of xsd being used to validate an xml file, but what I really want is to just use one element of the xsd to validate some user input.
Is there a simple way to do this or should I just treat the xsd file as an xml file, extract the element and compare it to the given string to see if it's valid?
Thanks
If I'm understanding your question correctly, you typically use jaxb along with an xsd(schema) to validate an xml file not the contents of a node in an xml file. You may be better off using xpath to parse the xml file and get the contents of the particular node and then do your comparison that way.
Here is a link to one of the jaxb tutorials and a linkt to an XPATH tutorial.