<<Think in Java 4 >>中关于使用XOM操作xml文件

xom类库位置http://www.xom.nu/

生成:

package xml;
// Use the XOM library to write and read XML
// {Requires: nu.xom.Node; You must install
// the XOM library from http://www.xom.nu }
import nu.xom.*;
import java.io.*;
import java.util.*;


public class Person {
  private String first, last;
  public Person(String first, String last) {
    this.first = first;
    this.last = last;
  }
  // Produce an XML Element from this Person object:
  public Element getXML() {
    Element person = new Element("person");
    Element firstName = new Element("first");
    firstName.appendChild(first);
    Element lastName = new Element("last");
    lastName.appendChild(last);
    person.appendChild(firstName);
    person.appendChild(lastName);
    return person;
  }
  // Constructor to restore a Person from an XML Element:
  public Person(Element person) {
    first= person.getFirstChildElement("first").getValue();
    last = person.getFirstChildElement("last").getValue();
  }
  public String toString() { return first + " " + last; }
  // Make it human-readable:
  public static void
  format(OutputStream os, Document doc) throws Exception {
    Serializer serializer= new Serializer(os,"ISO-8859-1");
    serializer.setIndent(4);
    serializer.setMaxLength(60);
    serializer.write(doc);
    serializer.flush();
  }
  public static void main(String[] args) throws Exception {
    List<Person> people = Arrays.asList(
      new Person("Dr. Bunsen", "Honeydew"),
      new Person("Gonzo", "The Great"),
      new Person("Phillip J.", "Fry"));
    System.out.println(people);
    Element root = new Element("people");
    for(Person p : people)
      root.appendChild(p.getXML());
    Document doc = new Document(root);
    format(System.out, doc);
    format(new BufferedOutputStream(new FileOutputStream(
      "People.xml")), doc);
  }


处理

package xml;
// {Requires: nu.xom.Node; You must install
// the XOM library from http://www.xom.nu }
// {RunFirst: Person}
import java.io.File;
import java.util.ArrayList;


import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Elements;


public class People extends ArrayList<Person> {
  public People(String fileName) throws Exception  {
    Document doc = new Builder().build(new File(fileName));
    Elements elements =
      doc.getRootElement().getChildElements();
    for(int i = 0; i < elements.size(); i++)
      add(new Person(elements.get(i)));
  }
  public static void main(String[] args) throws Exception {
    People p = new People("People.xml");
    System.out.println(p);
  }
}

xml转json时的依赖包, Size: 421.45 KB Detail: META-INF/ META-INF/MANIFEST.MF 1.15 KB nu/ nu/xom/ nu/xom/ASCIIWriter.class 499.0 bytes nu/xom/Attribute$Type.class 2.04 KB nu/xom/Attribute.class 8.11 KB nu/xom/Builder$1.class 176.0 bytes nu/xom/Builder$NamespaceWellformednessRequired.class 1.14 KB nu/xom/Builder$ValidityRequired.class 1.45 KB nu/xom/Builder.class 14.95 KB nu/xom/CDATASection.class 1.04 KB nu/xom/Comment.class 2.75 KB nu/xom/CycleException.class 593.0 bytes nu/xom/DocType.class 5.98 KB nu/xom/Document.class 4.74 KB nu/xom/DocumentFragment.class 968.0 bytes nu/xom/EBCDICWriter.class 787.0 bytes nu/xom/Element$1.class 1.08 KB nu/xom/Element.class 18.97 KB nu/xom/Elements.class 829.0 bytes nu/xom/GenericWriter.class 1.8 KB nu/xom/ISOArabicWriter.class 1.25 KB nu/xom/ISOCyrillicWriter.class 1.5 KB nu/xom/ISOGreekWriter.class 1.58 KB nu/xom/ISOHebrewWriter.class 1.13 KB nu/xom/ISOThaiWriter.class 1.38 KB nu/xom/IllegalAddException.class 612.0 bytes nu/xom/IllegalCharacterDataException.class 639.0 bytes nu/xom/IllegalDataException.class 820.0 bytes nu/xom/IllegalNameException.class 612.0 bytes nu/xom/IllegalTargetException.class 618.0 bytes nu/xom/Info.class 1.74 KB nu/xom/JDK15XML1_0Parser.class 588.0 bytes nu/xom/JaxenConnector.class 1.6 KB nu/xom/JaxenNavigator$ChildIterator.class 1.82 KB nu/xom/JaxenNavigator$NamedChildIterator.class 1.81 KB nu/xom/JaxenNavigator.class 9.43 KB nu/xom/Latin10Writer.class 2.74 KB nu/xom/Latin1Writer.class 503.0 bytes nu/xom/Latin2Writer.class 2.88 KB nu/xom/Latin3Writer.class 2.64 KB nu/xom/Latin4Writer.class 2.86 KB nu/xom/Latin5Writer.class 1.91 KB nu/xom/Latin6Writer.class 2.69 KB nu/xom/Latin7Writer.class 2.75 KB nu/xom/Latin8Writer.class 3.16 KB nu/xom/Latin9Writer.class 1.84 KB nu/xom/MalformedURIException.class 615.0 bytes nu/xom/MultipleParentException.class 620.0 bytes nu/xom/Namespace.class 2.37 KB nu/xom/NamespaceConflictException.class 633.0 bytes nu/xom/Namespaces.class 1.66 KB nu/xom/NoSuchAttributeException.class 684.0 bytes nu/xom/NoSuchChildException.class 672.0 bytes nu/xom/Node$1.class 167.0 bytes nu/xom/Node$EmptyNamespaceContext.class 740.0 bytes nu/xom/Node.class 4.87 KB nu/xom/NodeFactory.class 3.07 KB nu/xom/Nodes.class 1.54 KB nu/xom/NonVerifyingHandler.class 5.29 KB nu/xom/ParentNode.class 3.7 KB nu/xom/ParsingException.class 2.79 KB nu/xom/ProcessingInstruction.class 3.68 KB nu/xom/Serializer.class 12.2 KB nu/xom/Text.class 4.27 KB nu/xom/TextWriter.class 9.19 KB nu/xom/TextWriterFactory.class 2.6 KB nu/xom/UCSWriter.class 517.0 bytes nu/xom/URIUtil$ParsedURI.class 2.06 KB nu/xom/URIUtil.class 6.17 KB nu/xom/UnavailableCharacterException.class 1.14 KB nu/xom/UnicodeUtil$UnicodeString.class 3.19 KB nu/xom/UnicodeUtil.class 111.86 KB nu/xom/UnicodeWriter.class 3.79 KB nu/xom/UnsynchronizedBufferedWriter.class 1.78 KB nu/xom/ValidityException.class 2.59 KB nu/xom/Verifier$1.class 663.0 bytes nu/xom/Verifier$EmptyReader.class 782.0 bytes nu/xom/Verifier$URICache.class 1020.0 bytes nu/xom/Verifier.class 17.8 KB nu/xom/WellformednessException.class 613.0 bytes nu/xom/XML1_0Parser.class 422.0 bytes nu/xom/XMLException.class 1.07 KB nu/xom/XOMHandler.class 15.12 KB nu/xom/XPathContext$1.class 191.0 bytes nu/xom/XPathContext$JaxenNamespaceContext.class 1.06 KB nu/xom/XPathContext.class 2.04 KB nu/xom/XPathException.class 1.29 KB nu/xom/XPathTypeException.class 641.0 bytes nu/xom/benchmarks/ nu/xom/canonical/ nu/xom/canonical/CanonicalizationException.class 1003.0 bytes nu/xom/canonical/Canonicalizer$1.class 214.0 bytes nu/xom/canonical/Canonicalizer$AttributeComparator.class 1.25 KB nu/xom/canonical/Canonicalizer$CanonicalXMLSerializer.class 11.33 KB nu/xom/canonical/Canonicalizer.class 6.63 KB nu/xom/characters.dat 64.0 KB nu/xom/compositions.dat 21.5 KB nu/xom/converters/ nu/xom/converters/DOMConverter.class 10.39 KB nu/xom/converters/SAXConverter.class 6.08 KB nu/xom/samples/ nu/xom/tests/ nu/xom/tests/XOMTestCase.class 7.85 KB nu/xom/xinclude/ nu/xom/xinclude/BadEncodingAttributeException.class 634.0 bytes nu/xom/xinclude/BadHTTPHeaderException.class 613.0 bytes nu/xom/xinclude/BadHrefAttributeException.class 622.0 bytes nu/xom/xinclude/BadParseAttributeException.class 625.0 bytes nu/xom/xinclude/EncodingHeuristics.class 2.58 KB nu/xom/xinclude/InclusionLoopException.class 613.0 bytes nu/xom/xinclude/MisplacedFallbackException.class 625.0 bytes nu/xom/xinclude/NoIncludeLocationException.class 867.0 bytes nu/xom/xinclude/XIncludeException.class 1.36 KB nu/xom/xinclude/XIncluder.class 15.85 KB nu/xom/xinclude/XPointer.class 5.65 KB nu/xom/xinclude/XPointerException.class 1.12 KB nu/xom/xinclude/XPointerResourceException.class 477.0 bytes nu/xom/xinclude/XPointerSyntaxException.class 645.0 bytes nu/xom/xslt/ nu/xom/xslt/XOMInputSource.class 475.0 bytes nu/xom/xslt/XOMReader.class 3.52 KB nu/xom/xslt/XOMResult.class 778.0 bytes nu/xom/xslt/XOMSource.class 1.05 KB nu/xom/xslt/XSLException.class 1.08 KB nu/xom/xslt/XSLTHandler.class 7.8 KB nu/xom/xslt/XSLTransform$1.class 201.0 bytes nu/xom/xslt/XSLTransform$FatalListener.class 1.03 KB nu/xom/xslt/XSLTransform.class 5.94 KB org/ org/jaxen/ org/jaxen/BaseXPath.class 6.67 KB org/jaxen/Context.class 2.48 KB org/jaxen/ContextSupport.class 2.8 KB org/jaxen/DefaultNavigator.class 4.08 KB org/jaxen/Function.class 240.0 bytes org/jaxen/FunctionCallException.class 894.0 bytes org/jaxen/FunctionContext.class 282.0 bytes org/jaxen/JaxenConstants.class 677.0 bytes org/jaxen/JaxenException.class 1.2 KB org/jaxen/JaxenHandler.class 10.75 KB org/jaxen/JaxenRuntimeException.class 1.63 KB org/jaxen/NamedAccessNavigator.class 394.0 bytes org/jaxen/NamespaceContext.class 204.0 bytes org/jaxen/Navigator.class 2.0 KB org/jaxen/QualifiedName.class 1.17 KB org/jaxen/SimpleFunctionContext.class 1.57 KB org/jaxen/SimpleNamespaceContext.class 2.36 KB org/jaxen/SimpleVariableContext.class 1.72 KB org/jaxen/UnresolvableException.class 380.0 bytes org/jaxen/UnsupportedAxisException.class 385.0 bytes org/jaxen/VariableContext.class 285.0 bytes org/jaxen/XPath.class 1.04 KB org/jaxen/XPathFunctionContext.class 3.52 KB org/jaxen/XPathSyntaxException.class 1.52 KB org/jaxen/expr/ org/jaxen/expr/AdditiveExpr.class 198.0 bytes org/jaxen/expr/AllNodeStep.class 145.0 bytes org/jaxen/expr/BinaryExpr.class 203.0 bytes org/jaxen/expr/CommentNodeStep.class 153.0 bytes org/jaxen/expr/DefaultAbsoluteLocationPath.class 2.19 KB org/jaxen/expr/DefaultAdditiveExpr.class 990.0 bytes org/jaxen/expr/DefaultAllNodeStep.class 1.38 KB org/jaxen/expr/DefaultAndExpr.class 1.81 KB org/jaxen/expr/DefaultArithExpr.class 836.0 bytes org/jaxen/expr/DefaultBinaryExpr.class 1.65 KB org/jaxen/expr/DefaultCommentNodeStep.class 1.65 KB org/jaxen/expr/DefaultDivExpr.class 1.46 KB org/jaxen/expr/DefaultEqualityExpr.class 2.99 KB org/jaxen/expr/DefaultEqualsExpr.class 1.52 KB org/jaxen/expr/DefaultExpr.class 973.0 bytes org/jaxen/expr/DefaultFilterExpr.class 3.35 KB org/jaxen/expr/DefaultFunctionCallExpr.class 3.71 KB org/jaxen/expr/DefaultGreaterThanEqualExpr.class 1.02 KB org/jaxen/expr/DefaultGreaterThanExpr.class 1.0 KB org/jaxen/expr/DefaultLessThanEqualExpr.class 1.01 KB org/jaxen/expr/DefaultLessThanExpr.class 1020.0 bytes org/jaxen/expr/DefaultLiteralExpr.class 1.25 KB org/jaxen/expr/DefaultLocationPath.class 3.07 KB org/jaxen/expr/DefaultLogicalExpr.class 462.0 bytes org/jaxen/expr/DefaultMinusExpr.class 1.46 KB org/jaxen/expr/DefaultModExpr.class 1.46 KB org/jaxen/expr/DefaultMultiplicativeExpr.class 912.0 bytes org/jaxen/expr/DefaultMultiplyExpr.class 1.48 KB org/jaxen/expr/DefaultNameStep.class 6.48 KB org/jaxen/expr/DefaultNotEqualsExpr.class 1.54 KB org/jaxen/expr/DefaultNumberExpr.class 1.32 KB org/jaxen/expr/DefaultOrExpr.class 1.8 KB org/jaxen/expr/DefaultPathExpr.class 2.54 KB org/jaxen/expr/DefaultPlusExpr.class 1.45 KB org/jaxen/expr/DefaultPredicate.class 1.6 KB org/jaxen/expr/DefaultProcessingInstructionNodeStep.class 1.76 KB org/jaxen/expr/DefaultRelationalExpr.class 2.87 KB org/jaxen/expr/DefaultRelativeLocationPath.class 885.0 bytes org/jaxen/expr/DefaultStep.class 3.56 KB org/jaxen/expr/DefaultTextNodeStep.class 1.46 KB org/jaxen/expr/DefaultTruthExpr.class 1.75 KB org/jaxen/expr/DefaultUnaryExpr.class 1.87 KB org/jaxen/expr/DefaultUnionExpr.class 2.65 KB org/jaxen/expr/DefaultVariableReferenceExpr.class 1.88 KB org/jaxen/expr/DefaultXPathExpr.class 1.63 KB org/jaxen/expr/DefaultXPathFactory.class 8.52 KB org/jaxen/expr/EqualityExpr.class 198.0 bytes org/jaxen/expr/Expr.class 364.0 bytes org/jaxen/expr/FilterExpr.class 318.0 bytes org/jaxen/expr/FunctionCallExpr.class 319.0 bytes org/jaxen/expr/IdentitySet$IdentityWrapper.class 795.0 bytes org/jaxen/expr/IdentitySet.class 866.0 bytes org/jaxen/expr/LiteralExpr.class 189.0 bytes org/jaxen/expr/LocationPath.class 259.0 bytes org/jaxen/expr/LogicalExpr.class 196.0 bytes org/jaxen/expr/MultiplicativeExpr.class 210.0 bytes org/jaxen/expr/NameStep.class 205.0 bytes org/jaxen/expr/NodeComparator.class 2.15 KB org/jaxen/expr/NumberExpr.class 186.0 bytes org/jaxen/expr/PathExpr.class 300.0 bytes org/jaxen/expr/Predicate.class 443.0 bytes org/jaxen/expr/PredicateSet.class 3.87 KB org/jaxen/expr/Predicated.class 304.0 bytes org/jaxen/expr/ProcessingInstructionNodeStep.class 222.0 bytes org/jaxen/expr/RelationalExpr.class 202.0 bytes org/jaxen/expr/Step.class 591.0 bytes org/jaxen/expr/TextNodeStep.class 147.0 bytes org/jaxen/expr/UnaryExpr.class 185.0 bytes org/jaxen/expr/UnionExpr.class 147.0 bytes org/jaxen/expr/VariableReferenceExpr.class 234.0 bytes org/jaxen/expr/Visitable.class 161.0 bytes org/jaxen/expr/Visitor.class 995.0 bytes org/jaxen/expr/VisitorSupport.class 3.0 KB org/jaxen/expr/XPathExpr.class 415.0 bytes org/jaxen/expr/XPathFactory.class 2.06 KB org/jaxen/expr/iter/ org/jaxen/expr/iter/IterableAncestorAxis.class 858.0 bytes org/jaxen/expr/iter/IterableAncestorOrSelfAxis.class 882.0 bytes org/jaxen/expr/iter/IterableAttributeAxis.class 1.5 KB org/jaxen/expr/iter/IterableAxis.class 1.23 KB org/jaxen/expr/iter/IterableChildAxis.class 1.48 KB org/jaxen/expr/iter/IterableDescendantAxis.class 866.0 bytes org/jaxen/expr/iter/IterableDescendantOrSelfAxis.class 890.0 bytes org/jaxen/expr/iter/IterableFollowingAxis.class 862.0 bytes org/jaxen/expr/iter/IterableFollowingSiblingAxis.class 890.0 bytes org/jaxen/expr/iter/IterableNamespaceAxis.class 862.0 bytes org/jaxen/expr/iter/IterableParentAxis.class 850.0 bytes org/jaxen/expr/iter/IterablePrecedingAxis.class 862.0 bytes org/jaxen/expr/iter/IterablePrecedingSiblingAxis.class 890.0 bytes org/jaxen/expr/iter/IterableSelfAxis.class 842.0 bytes org/jaxen/function/ org/jaxen/function/BooleanFunction.class 1.46 KB org/jaxen/function/CeilingFunction.class 1.26 KB org/jaxen/function/ConcatFunction.class 1.51 KB org/jaxen/function/ContainsFunction.class 1.44 KB org/jaxen/function/CountFunction.class 1.04 KB org/jaxen/function/FalseFunction.class 881.0 bytes org/jaxen/function/FloorFunction.class 1.25 KB org/jaxen/function/IdFunction.class 2.19 KB org/jaxen/function/LangFunction.class 2.95 KB org/jaxen/function/LastFunction.class 942.0 bytes org/jaxen/function/LocalNameFunction.class 1.85 KB org/jaxen/function/NameFunction.class 1.83 KB org/jaxen/function/NamespaceUriFunction.class 1.85 KB org/jaxen/function/NormalizeSpaceFunction.class 1.7 KB org/jaxen/function/NotFunction.class 1.2 KB org/jaxen/function/NumberFunction.class 2.12 KB org/jaxen/function/PositionFunction.class 968.0 bytes org/jaxen/function/RoundFunction.class 1.34 KB org/jaxen/function/StartsWithFunction.class 1.45 KB org/jaxen/function/StringFunction.class 3.74 KB org/jaxen/function/StringLengthFunction.class 1.96 KB org/jaxen/function/SubstringAfterFunction.class 1.49 KB org/jaxen/function/SubstringBeforeFunction.class 1.48 KB org/jaxen/function/SubstringFunction.class 2.43 KB org/jaxen/function/SumFunction.class 1.51 KB org/jaxen/function/TranslateFunction.class 3.57 KB org/jaxen/function/TrueFunction.class 876.0 bytes org/jaxen/function/ext/ org/jaxen/function/ext/EndsWithFunction.class 1.45 KB org/jaxen/function/ext/EvaluateFunction.class 2.45 KB org/jaxen/function/ext/LocaleFunctionSupport.class 2.13 KB org/jaxen/function/ext/LowerFunction.class 1.61 KB org/jaxen/function/ext/MatrixConcatFunction$MatrixEnum.class 1.57 KB org/jaxen/function/ext/MatrixConcatFunction.class 2.23 KB org/jaxen/function/ext/UpperFunction.class 1.61 KB org/jaxen/function/xslt/ org/jaxen/function/xslt/DocumentFunction.class 1.31 KB org/jaxen/saxpath/ org/jaxen/saxpath/Axis.class 1.75 KB org/jaxen/saxpath/Operator.class 557.0 bytes org/jaxen/saxpath/SAXPathEventSource.class 262.0 bytes org/jaxen/saxpath/SAXPathException.class 2.22 KB org/jaxen/saxpath/XPathHandler.class 1.88 KB org/jaxen/saxpath/XPathReader.class 268.0 bytes org/jaxen/saxpath/XPathSyntaxException.class 1.75 KB org/jaxen/saxpath/base/ org/jaxen/saxpath/base/Token.class 1.65 KB org/jaxen/saxpath/base/TokenTypes.class 2.37 KB org/jaxen/saxpath/base/Verifier.class 7.09 KB org/jaxen/saxpath/base/XPathLexer.class 7.52 KB org/jaxen/saxpath/base/XPathReader.class 12.38 KB org/jaxen/saxpath/helpers/ org/jaxen/saxpath/helpers/DefaultXPathHandler.class 4.56 KB org/jaxen/saxpath/helpers/XPathReaderFactory.class 2.71 KB org/jaxen/util/ org/jaxen/util/AncestorAxisIterator.class 525.0 bytes org/jaxen/util/AncestorOrSelfAxisIterator.class 1.3 KB org/jaxen/util/DescendantAxisIterator.class 1.73 KB org/jaxen/util/DescendantOrSelfAxisIterator.class 625.0 bytes org/jaxen/util/FollowingAxisIterator.class 1.96 KB org/jaxen/util/FollowingSiblingAxisIterator.class 1.52 KB org/jaxen/util/PrecedingAxisIterator.class 2.46 KB org/jaxen/util/PrecedingSiblingAxisIterator.class 1.9 KB org/jaxen/util/SelfAxisIterator.class 383.0 bytes org/jaxen/util/SingleObjectIterator.class 858.0 bytes org/jaxen/util/SingletonList.class 888.0 bytes org/w3c/ org/w3c/dom/ org/w3c/dom/UserDataHandler.class 419.0 bytes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值