Java Web Base Disk - Other Articles

XML

Operation of XML Files

Conversion between XML and JavaBean

1. File operation

1)Create an empty XmlDoc

    XmlDoc doc = new XmlDoc();

2)Use the external input element as the root element

    Element element = ...

    XmlDoc doc = new XmlDoc(element);

3)With the specified external file or xml Text string creation Document

    XmlDoc doc = new XmlDoc("conf/xmldoc_ut.xml",true);

    String text = "<?xml version=1.0 encoding=UTF-8 ?>";

    text  = "<root><name>value</name></root>";

    XmlDoc doc = new XmlDoc(text,false);

4)Create external incoming flows Document

    InputStream inputStream = ...

    XmlDoc doc = new XmlDoc(inputStream);

2. Read, write, modify and delete the created documents

1) Operation based on elements and attributes

    All operations are based on the current element, so the current element must be set before each use.

    // Operate under the root element

    doc.setCurrentElement(doc.readRootElement());

    // Various operations under the first boot element

    doc.setCurrentElement(doc.readFirstElement("book"));



    This approach is generally used to traverse an xml document or to create a new xml document from scratch

2) Operation mode based on Xpath

    // Set the second booktype element under the current node as the current node

    doc.setCurrentNode(doc.readNode("booktype[2]"));

    // Modify all book element values under the current node

    doc.modifyNodes("book","xpath");



    Xpath is generally used to query and modify existing documents

3. Document output

1)output file

    doc.exportDocument("d:/text.xml");

2)output stream

    InputStream input = doc.exportDocument();

3)Output as text string

    String text = doc.exportText(false); //false denotes no indentation newline format, etc.

4. Conversion between Xml and JavaBean

The XmlUtil class is encapsulated to complete the conversion between Xml and Java objects.

public static<T> String toXml(T t);

public static<T> T fromXml(String xml,Class<?>... classes);



String s = XmlUtil.toXml(list);

User user = XmlUtil.fromXml(xml);

JSON

Operation of json file

Conversion between Json and Java Objects

1. File operation

1)Establish JsonDoc Example

    //There are three kinds of output: file, stream and string.

    JsonDoc json = new JsonDoc("d:/test.json");

    JsonDoc json = new JsonDoc(OutputStream);

    JsonDoc json = new JsonDoc(StringWriter);

2)Write in Json

    //Write arrays, objects, or values according to the Json structure

    //1. start

    json.writeStartArray();



    json.writeStartObject();

    //1.1 start

    json.writeObjectFieldStart("1.1 name");

    json.writeStringField("first","john");

    json.writeStringField("last","pack");

    //1.1 end

    json.writeEndObject();

    //1.2 start

    json.writeStartOjbect();

    json.writeStringField("1.1","xxxx");

    json.writeNumberField("1.2",1.2f);

    //1.2 end

    json.writeEndObject();

    //2. end

    json.writeEndArray();



    //3. Close JsonDoc

    json.close();

2. Intertransformation between Json and Java Objects

1)Map and Json

    public static<V> String toJson(Map<String,V> map)       ;

    public static Map<String,Map<String,Object>> toMap(String json);

2)Bean and Json

    public static<T> String toJson(T t);

    public static<T> T toBean(String json,Class<T> clazz);

3)List and Json

    public static<T> String toJson(List<T> list);

    public static<T> List<T> toList(String json,Class<T> clazz);



JsonUtil.toJson(esJson,Employee.class);

PDF

TPdfReader: Reading Interactive Elements in Templates

TPdfWrite: Modifying Interactive Elements in Templates

1.TPdfReader

Get the list of interactive elements id

Get the value of the interactive element

Get the type of interactive element

Returns candidates in drop-down lists or list boxes as arrays



1) Instantiate TPdfReader

TPdfReader reader = new TPdfReader("D:/PdfTep.pdf");

2) Call method through TPdfReader instance

String value = reader.getAcroFieldValue("Form 1");

Blog from Zhang Yongguang

Keywords: JSON xml Java encoding

Added by bben95 on Sun, 19 May 2019 10:57:29 +0300