Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've an xml like which looks like this one:
<rootElement>
<title> randmonString </title>
<subElement1>
<someInfo> info </someInfo>
<subElemntTrash> trash </subElementTrash>
<someInfo1> info1 </someInfo1>
</subElement1>
<trash>
<subtrash> trash </subtrash>
</trash>
<date> 19.03.15 </date>
</rootElement>
I need to extract only title, some /subElement1/subInfo, /subElement1/subInfo1 and date, rest should be automatically stored somewhere but without those elements, that were already extracted. I also should have possibility to marshal it back to the original xml.
It would be great if it can be done using annotation mapping.
Can someone give me the right direction to search?
You are asking about parsing, but then you want data extraction, data transformation and finally storing in some undefined form. Very broad question with many possible aporaches.
You can parse XML in java using DOM, SAX, StAX.
You can use XPath to extract interesting information, but it will not divide your document into the interesting bit and the 'rest'.
You can define XSLT templates, to initiate java Transformer, in order to split your input document into the interesting and 'the rest' parts.
You can use JAXB to map the xml into an java model (using your favourite the annotation mapping), and then you can build another representations containing your interesting and 'the rest' part. Then you can save both representation to different xml.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am a newbie in Java and I have a unique requirement. I am getting xml data from client and I had already defined schema with me. Now, the problem is client side xml has many more element than schema I have. How to deal with it?
Further, there are some elements which have different architecture then what is defined in schema, I want to transform it into form of what schema has. Does it sound wrong since schema is generally there for validating? Can someone give me overview of how to deal with the problem with small example?
The purpose of an XML schema is to describe the structure of a set of XML documents. If your client is sending XML which does not conform to your schema, then one of three things must be true:
The client is incorrect. If so, you should reject their request as it is not valid.
Your schema is incorrect. In this case, you should correct your schema so that it describes exactly what is permitted in requests.
Your schema is describing something else entirely. In this case, you should write another schema which does describe the permitted requests. You may want then want to transform documents matching this schema into documents matching your new schema - XSLT is a great tool for doing exactly this.
I notice you've tagged JAXB too - another way to perform the transformation would be to construct JAXB representations of both schemas and perform the conversion yourself in code.
Is this helpful? I'd say first and foremost, you need to work out what role your XML schema is performing in your system, and that's a design task you should do before you start writing any code.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am looking for a standard technology to drive the generation of an XML document based on an XSD and a set of rules. Basically I have XSDs that tell me what the XML should look like and what elements are mandatory or optional. What is not in the XSDs is a set of business rules that say things like "if such element's value is this, that other element is actually mandatory" or "if such element's value is that, that other element should be omitted".
What I have in mind is something that would process the XSDs along with the rules (maybe expressed in something like XPath) and call back my code to generate the mandatory values. The structure of the final document would change dynamically depending on the values of the elements driving the conditions.
I guess I could do something close to what I want with XSLTs. I'd generate all the values with and then use an XSLT to enforce the conditions. But in my case some values maybe take long to produce so I want to avoid computing unnecessary values, meaning values that will be later discarded by the business rules.
Does such a technology exist? FYI I am coding in Java but I am hoping to find a generic technology if possible.
Cheers,
Tom
The problem you described can probably be handled by Schematron. It can be used with XML Schema, and if you already know XPath and XSLT you won't find it difficult to understand. If can specify complex relationships between unrelated nodes based on values and context beyond the abilities of XML Schema.
The specification and many tutorials you can find in the Schematron website.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I just started to figure out what regexp is, but I have really limited time!
I have a string in xml like : <myid>1234</myid>, for now my xml is in txt it used to be an xml document.
How can I make pattern to extract 1234 from <myid> tag.
If it really looks like this:
<myid>1234</myid>
...you can extract it like this:
Matcher match = Pattern.compile("<myid>(\d+)</myid>").matcher(str);
...and then use the matcher repeatedly, getting the value from the capture group.
But there's a reason everyone is telling you to use a proper parser. There are lots of ways the above can fail, both matching inappropriately and failing to match when it should.
The proper solution is to make the XML valid, and then parse it, and use XPath or similar to read the values.
If you really have some tool requiring you to send it invalid XML, you need to replace that tool. More likely, though, it's some misunderstanding.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a huge problem with parsing an XML file to a different format.
I'm trying to get all the related data like stated in this link: http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
(I searched stackoverflow before and found this link)
I use the interface XMLReader to parse and the XML Serializer for the output.
I just need to convert my XML with a DTD to another XML with a different DTD. The difference is that, instead of elements from my source XML, most of the children are now attributes in the target XML. There are no new elements, only a different arrangement.
Has anyone an idea how to deal with the problem with a SAX parser?
You can use XMLFilters for that. See Elliotte Rusty Harold's book for explanation and examples:
The basic idea of filters is that an XMLReader, instead of receiving
XML text directly from a file, socket, or other source, receives
already parsed events from another XMLReader. It can change these
events before passing them along to the client application through the
usual methods of ContentHandler and the other callback interfaces. For
example, it can add a unique ID attribute to every element or delete
all elements in the SVG namespace from the input stream.
BTW the mkyong tutorial glosses over how the characters method works, that tends to bite a lot of people when they find their element data getting truncated. There's a better tutorial on Oracle's site.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have been learning how to build android apps this summer. I am currently trying to work on xml parsing which falls under java in this case. I have a few questions that are mostly conceptual and one specific one.
First, in most of the examples I have seen pages already in xml are used. Can I use a page in regular html format and with whatever the program does turn it to xml and then parse it? Or is that what is normally done anyway?
Secondly, I could use a little explanation on how the parser actually works and saves the data so I will better know how to use it (extract it from whatever it is saved in), when the parsing is done.
So for my specific example I am trying to work with some weather data from the NWS. My program will take the data from this page, and after some user input take you to a page like this, which sometimes will have various alerts. I want to select certain ones. This is what I could use help with. I haven't really coded anything on that yet because I don't know what I am doing.
If I need to clarify or rephrase anything in here I am happy too and let me know. I am trying to be a good contributor on here!
Yes you can parse HTML and there are many parsers available too, there is a question about it here Parse HTML in Android, then we have an answer here about parsing html https://stackoverflow.com/a/7114346/826657
Although its a bad idea, as the tag names aren't well named, so you will have to write lots of code searching attributes for a specific data tag, so you always have to prefer XML,for saving lots of code space and also time.
Here is a text from CodingHorror which says at general parsing html is a bad idea.
http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html
Here is something which explains parsing an XML document using XML PullParser http://www.ibm.com/developerworks/library/x-android/