In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use XML documents for analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
How to use XML documents for analysis
I. push analysis to pull analysis
Compared with push analysis, pull analysis has the following advantages:
1. In pull analysis, events are generated by the analysis application, so the analysis rules are provided to the client rather than the parser.
two。 The code for pull analysis is simpler and it has fewer libraries than push analysis.
3. Pull analysis client can read multiple XML documents at the same time.
4. Pull analysis allows you to filter XML documents and skip analysis events.
Second, understand StAX
Streaming API (StAX) for XML was introduced in the JSR173 specification in March 2004, which is a streaming analysis of XML API.StAX is a new feature provided by JDK6.0, you can download its test version here.
A push model parser continues to generate events until the XML document is fully parsed. However, pull analysis is adjusted by the application; therefore, the analysis event is generated by the application. This means that with StaX, you can defer parsing-skip elements during parsing and analyze multiple documents. When using DOMAPI, you must parse the entire XML document into a DOM structure, which reduces the efficiency of the analysis. With the help of StAX, an analysis event is generated when parsing an XML document. Comparisons between StAX parsers and other parsers are not described here.
StAXAPI is implemented using JavaWeb Services Development (JWSDP) 1.6, combined with SunJava streaming XML Analyzer (SJSXP), which is located in the javax.xml.stream package. The XMLStreamReader interface is used to parse an XML document, while the XMLStreamWriter interface is used to generate an XML document. XMLEventReader is responsible for analyzing XML events using an object event iterator-in contrast to the cursor mechanism used by XMLStreamReader. This tutorial will analyze an XML document based on the StAX implementation in JDK6.0.
In fact, StaX is just one of the new features of XML provided by JDK6.0. The new JDK6.0 also provides support for Java Schema (JAX-WS) 2.0 for XML-Web services, JavaAPI (JAXB) 2.0 XML digital signature API for XML binding, and even SQL:2003'XML' data types.
III. Preliminary installation
If you are using JDK6.0, StAXAPI is located in Classpath by default. If you are using JWSDP1.6, please add JWSDP1.6StAXAPI to classpath. This requires adding sjsxplibjsr173_api.jar and sjsxplibsjsxp.jar to the CLASSPATH variable. Install JWSDP1.6.Jsr173_api.jar in the directory corresponding to JSR-173APIJAR,Sjsxp.jar corresponding to SJXSP to implement JAR.
4. Use XMLStreamWriter for write operation
First, you need to create the XML document that will be analyzed. XML. XML is generated by XMLStreamWriter of StAX. One limitation of XMLStreamWriter, however, is that it does not necessarily generate well-formed documents-and the resulting documents are not necessarily valid. You need to make sure that the generated XML document is well-formed. Listing 1 is an example of the original XML document generated by XMLStreamWriter.
How to use XML documents for analysis
Here, you try to use XMLStreamWriterAPI to generate catalog.xml. Exe in listing 1. The code snippet in this section is excerpted from the XMLWriter.java application and is shown in listing 2. First, you will import the StAX package class, please refer to the following encoding:
Importjavax.xml.stream.*
Importjavax.xml.stream.events.*
Importjavax.xml.stream.XMLOutputFactory
You need to get your XMLStreamWriter from a XMLOutputFactory. So, first you must create a new XMLOutputFactory:
XMLOutputFactoryoutputFactory=XMLOutputFactory.newInstance ()
Next, create a FileWriter to output the XML document-it will be generated into a XML file:
FileWriteroutput=newFileWriter (newFile ("C:/STAX/catalog.xml"))
Next, create a XMLStreamWriter:
XMLStreamWriterXMLStreamWriterr=outputFactory.createXMLStreamWriter (output)
Now, use the writeStartDocument () method to create a document beginning. Add the encoding and version to be specified in the XML declaration (remember that the specified encoding is not the encoding of the generated XML document). What if you need to specify the encoding of an XML document? When you create a XMLStreamWriter object from a XMLOutputFactory object, you do this:
XMLStreamWriter.writeStartDocument ("UTF-8", "1.0")
Use the writeComment () method to output a comment:
XMLStreamWriter.writeComment ("AOReillyJournalCatalog")
Use the writeProcessingInstruction () method to output a processing instruction:
XMLStreamWriter.writeProcessingInstruction ("catalog", "journal='OReilly'")
Use the writeStartElement () method to output the beginning of the 'catalog' element (element prefix and namespace URI can also be specified in this method):
XMLStreamWriter.writeStartElement ("journal", "catalog", "http://OnJava.com/Journal"")
Use the writeNamespace () method to add the 'journal' namespace declaration (namespace prefix and namespace URI are also specified in this method):
XMLStreamWriter.writeNamespace ("journal", "http://OnJava.com/Journal"")
Add the xsi namespace again using the writeNamespace () method:
XMLStreamWriter.writeNamespace ("xsi", "http://www.w3.org/2001/XMLSchema-instance"")
Use the writeAttribute () method to add the xsi:namespaceSchemaLocation property:
XMLStreamWriter.writeAttribute ("xsi:noNamespaceSchemaLocation", "file://c:/Schemas/catalog.xsd"")
Use the writeAttribute () method to add the 'publisher' property:
XMLStreamWriter.writeAttribute ("publisher", "OReilly")
Output the beginning of the 'journal' element. When a new element is added, the'> 'brackets of the previous element are also added:
XMLStreamWriter.writeStartElement ("journal", "journal", "http:
/ / OnJava.com/Journal ")
Use the writeAttribute () method to add the 'date' and' title' properties. Then, use the writeElement () method to add the 'article' and' title' elements. Then, use the writeCharacters () method to output the text of the 'title' element:
XMLStreamWriter.writeCharacters ("DataBindingwithXMLBeans")
Any element that contains text or child elements must have a closing tag. Use the writeEndElement () element to add the closing tag of the 'title' element:
XMLStreamWriter.writeEndElement ()
Add the closing tags of the 'author' element' and the 'journal' element. In the writeEndElement () method, it is not necessary to specify the element prefix and namespace URI. Add another 'journal' element in a similar manner. Then, add the closing tag of the 'catalog' element. Finally, output the buffered data:
XMLStreamWriter.flush ()
The last step is to shut down XMLStreamWriter.
XMLStreamWriter.close ()
This is the process of generating catalog.xml.
Listing 2 in the source code shows the complete Java application-XMLWriter.java. This application can be run as a command-line application or in an IDE such as Eclipse.
Fifth, use XMLStreamReader for analysis
Let's analyze how it works in detail by using XMLStreamReaderAPI to analyze the document in listing 1. XMLStreamReader uses a cursor to parse XML documents. Its interface contains a next () method that parses the next parsing event. The getEventType () method returns the event type. The following code snippet is from the XMLParser.java application, as detailed in listing 3.
In this XMLParser.java application, first, you need to import the StAX class:
Importjavax.xml.stream.*
Importjavax.xml.stream.events.*
Importjavax.xml.stream.XMLInputFactory
Then, create a XMLInputFactory, from which you get a XMLStreamReader:
XMLInputFactoryinputFactory=XMLInputFactory.newInstance ()
The above is all the contents of the article "how to use XML documents for Analysis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.