nu.xom.Serializer Java Examples

The following examples show how to use nu.xom.Serializer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XMLUtil.java    From rest-client with Apache License 2.0 8 votes vote down vote up
@Deprecated
public static String indentXML(final String in)
        throws XMLException, IOException {
    try {
        Builder parser = new Builder();
        Document doc = parser.build(in, null);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Serializer serializer = new Serializer(baos);
        serializer.setIndent(4);
        serializer.setMaxLength(69);
        serializer.write(doc);
        return new String(baos.toByteArray());
    } catch (ParsingException ex) {
        // LOG.log(Level.SEVERE, null, ex);
        throw new XMLException("XML indentation failed.", ex);
    }
}
 
Example #2
Source File: JunkUtils.java    From http4e with Apache License 2.0 6 votes vote down vote up
public static String prettyXml(String xml, String firstLine){
      try {
         
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         Serializer serializer = new Serializer(out);
         serializer.setIndent(2);
         if(firstLine != null){
            serializer.write(new Builder().build(firstLine + xml, ""));            
         } else {
            serializer.write(new Builder().build(xml, ""));
         }
         String ret =  out.toString("UTF-8");
         if(firstLine != null){
            return ret.substring(firstLine.length() , ret.length()).trim();
         } else {
            return ret;            
         }
      } catch (Exception e) {
//         ExceptionHandler.handle(e);
         return xml;
      }
   }
 
Example #3
Source File: XmlUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static String prettyXml(Document doc) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  Serializer serializer = new Serializer(out);
  serializer.setIndent(2);
  serializer.write(doc);
  return out.toString("UTF-8");
}
 
Example #4
Source File: XmlUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static String prettyXml(Document doc) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  Serializer serializer = new Serializer(out);
  serializer.setIndent(2);
  serializer.write(doc);
  return out.toString("UTF-8");
}
 
Example #5
Source File: XomCreateXML.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * 格式化
 */
@Test
public void test03() {
	BigInteger low  = BigInteger.ONE;
    BigInteger high = BigInteger.ONE;      

    Element root = new Element("Fibonacci_Numbers");  
    for (int i = 1; i <= 10; i++) {
        Element fibonacci = new Element("fibonacci");
        fibonacci.appendChild(low.toString());
        root.appendChild(fibonacci);
        
        BigInteger temp = high;
        high = high.add(low);
        low = temp;
    }
    Document doc = new Document(root);
      
    try {
      Serializer serializer = new Serializer(System.out, "ISO-8859-1");
      serializer.setIndent(4);
      serializer.setMaxLength(64);
      serializer.write(doc);  
    }
    catch (IOException ex) {
       System.err.println(ex); 
    }   
}
 
Example #6
Source File: XomCreateXML.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * 屬性
 */
@Test
public void test04() {
	BigInteger low = BigInteger.ONE;
	BigInteger high = BigInteger.ONE;

	Element root = new Element("Fibonacci_Numbers");
	for (int i = 1; i <= 10; i++) {
		Element fibonacci = new Element("fibonacci");
		fibonacci.appendChild(low.toString());
		Attribute index = new Attribute("index", String.valueOf(i));
		fibonacci.addAttribute(index);
		root.appendChild(fibonacci);

		BigInteger temp = high;
		high = high.add(low);
		low = temp;
	}
	Document doc = new Document(root);
	try {
		Serializer serializer = new Serializer(System.out, "ISO-8859-1");
		serializer.setIndent(4);
		serializer.setMaxLength(64);
		serializer.write(doc);
	} catch (IOException ex) {
		System.err.println(ex);
	}
}
 
Example #7
Source File: XomCreateXML.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * 聲明Document Type
 */
@Test
public void test05() {
	BigInteger low = BigInteger.ONE;
	BigInteger high = BigInteger.ONE;

	Element root = new Element("Fibonacci_Numbers");
	for (int i = 1; i <= 10; i++) {
		Element fibonacci = new Element("fibonacci");
		fibonacci.appendChild(low.toString());
		Attribute index = new Attribute("index", String.valueOf(i));
		fibonacci.addAttribute(index);
		root.appendChild(fibonacci);

		BigInteger temp = high;
		high = high.add(low);
		low = temp;
	}
	Document doc = new Document(root);
	DocType doctype = new DocType("Fibonacci_Numbers", "fibonacci.dtd");
	doc.insertChild(doctype, 0);
	try {
		Serializer serializer = new Serializer(System.out, "ISO-8859-1");
		serializer.setIndent(4);
		serializer.setMaxLength(64);
		serializer.write(doc);
	} catch (IOException ex) {
		System.err.println(ex);
	}
}
 
Example #8
Source File: XomCreateXML.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Create elements in namespaces
 */
@Test
public void test07() throws Exception{
	BigInteger low  = BigInteger.ONE;
      BigInteger high = BigInteger.ONE;      

      String namespace = "http://www.w3.org/1998/Math/MathML";
      Element root = new Element("mathml:math", namespace);  
      for (int i = 1; i <= 10; i++) {
        Element mrow = new Element("mathml:mrow", namespace);
        Element mi = new Element("mathml:mi", namespace);
        Element mo = new Element("mathml:mo", namespace);
        Element mn = new Element("mathml:mn", namespace);
        mrow.appendChild(mi);
        mrow.appendChild(mo);
        mrow.appendChild(mn);
        root.appendChild(mrow);
        mi.appendChild("f(" + i + ")");
        mo.appendChild("=");
        mn.appendChild(low.toString());
        
        BigInteger temp = high;
        high = high.add(low);
        low = temp;
      }
      Document doc = new Document(root);

      try {
        Serializer serializer = new Serializer(System.out, "ISO-8859-1");
        serializer.setIndent(4);
        serializer.setMaxLength(64);
        serializer.write(doc);  
      }
      catch (IOException ex) {
        System.err.println(ex); 
      }  
	
}