示例#1
0
 public static String syntax(Element element) {
   StringBuffer stringBuffer = new StringBuffer();
   int tab = 4, indent = 0;
   while (element != null) {
     String token = element.getToken();
     String description = token + ' ' + element.description() + '\n';
     int splitIdx = description.indexOf('\n');
     int spaces = indent * tab;
     while (splitIdx >= 0) {
       for (int idx = 0; idx < spaces; idx++) {
         stringBuffer.append(' ');
       }
       stringBuffer.append(description.substring(0, splitIdx));
       stringBuffer.append('\n');
       spaces = indent * tab + token.length() + 1;
       description = description.substring(splitIdx + 1);
       splitIdx = description.indexOf('\n');
     }
     if (element.getChild() != null) {
       indent = indent + 1;
       element = element.getChild();
     } else if (element.getSibling() != null) {
       element = element.getSibling();
     } else {
       while (element != null && element.getSibling() == null) {
         indent = indent - 1;
         element = element.getParent();
       }
       if (element != null) {
         element = element.getSibling();
       }
     }
   }
   return stringBuffer.toString();
 }
示例#2
0
	public boolean read(String strRoute, String strElement, int flag) {
		//SAXBuilder builder=new SAXBuilder();
		strText = null;
		try {
			String[] route = new String[4];

			String str = null;
			Document doc = builder.build(xmlFileName);
			Element root = doc.getRootElement();
			Element element = root;

			//创建一个拆分字符串内容的对象,每次返回一项
			StringTokenizer st = new StringTokenizer(strRoute, ":");
			str = st.nextToken();

			while (st.hasMoreTokens()) {
				str = st.nextToken();
				element = element.getChild(str);
			}

			//mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,str);
			element = (Element) element.getParent();
			/*
			 * while(flag!=1) { if(element.removeChild(str))
			 * mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"deleted "+str);
			 * else mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"not
			 * deleted");
			 * 
			 * flag--; }
			 */
			//	mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,element.getName());
			strText = element.getChild(str).getChild(strElement).getText();

		} catch (JDOMException jdome) {
			mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, xmlFileName
					+ " is not well-formed");

		} catch (IOException ioe) {
			mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, ioe);

		} catch (NullPointerException nullpe) {
			mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, "not founded"
					+ "\n" + nullpe);

		} catch (Exception e) {
			mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, "read no succeed"
					+ "\n" + e);

		}

		if (strText == null) {
			mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, "not founded");
			return false;
		} else {
			//	mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"strText="+strText);
			return true;
		}

	}
  /** {@inheritDoc } */
  @Override
  public JdomRepresentation remove(String nodeName) throws XmlException {

    Element candidate = getElement(nodeName);
    assert candidate != null : "getElement should have throw an execption";
    candidate.getParent().removeContent(candidate);
    return this;
  }
示例#4
0
 protected String getNamespaceByPrefix(String namespacePrefix) {
   for (Element current = this; current != null; current = current.getParent()) {
     String namespaceURI = current.getNamespaces().get(namespacePrefix);
     if (namespaceURI != null) {
       return namespaceURI;
     }
   }
   return null;
 }
示例#5
0
  /**
   * Indicates the end of an element (<code>&lt;/[element name]&gt;</code>) is reached. Note that
   * the parser does not distinguish between empty elements and non-empty elements, so this will
   * occur uniformly.
   *
   * @param namespaceURI <code>String</code> URI of namespace this element is associated with
   * @param localName <code>String</code> name of element without prefix
   * @param qName <code>String</code> name of element in XML 1.0 form
   * @throws SAXException when things go wrong
   */
  public void endElement(String namespaceURI, String localName, String qName) throws SAXException {

    if (suppress) return;

    flushCharacters();

    if (!atRoot) {
      Parent p = currentElement.getParent();
      if (p instanceof Document) {
        atRoot = true;
      } else {
        currentElement = (Element) p;
      }
    } else {
      throw new SAXException("Ill-formed XML document (missing opening tag for " + localName + ")");
    }
  }
示例#6
0
	/**
	 *  新增循环节点
	 * @param cycNode 循环节点
	 * @return
	 */
	public Element addCycNode(Element cycNode)
	{
		Element node=cycNode;
		Element child=null;
		Element newNode=null;
		Element tmpNode=null;
		String sName="";
		if(cycNode==null)
			return newNode;
		newNode=new Element(node.getName());
		node.getParent().addContent(newNode);
		List cList=node.getChildren();
		for(int i=0;i<cList.size();i++)
		{
			child=(Element)cList.get(i);
			sName=child.getName();
			sName=sName.trim();
			tmpNode=new Element(sName);
			newNode.addContent(tmpNode);
		}
		return newNode;
	}
示例#7
0
 protected Entry next() throws StaxNavException {
   try {
     if (next == null) {
       Element parent = element;
       while (true) {
         int type = stream.getEventType();
         if (type == XMLStreamConstants.START_ELEMENT) {
           next = new StreamEntry(stream, new Element(stream, parent));
           break;
         } else if (type == XMLStreamConstants.END_ELEMENT) {
           parent = parent.getParent();
           stream.next();
         } else if (type == XMLStreamConstants.END_DOCUMENT) {
           break;
         } else {
           stream.next();
         }
       }
     }
     return next;
   } catch (XMLStreamException e) {
     throw new StaxNavException(e);
   }
 }
示例#8
0
  @Override
  // Implementation methods
  // -------------------------------------------------------------------------
  protected Document parseDocument() throws DocumentException, IOException, XmlPullParserException {
    DocumentFactory df = getDocumentFactory();
    Document document = df.createDocument();
    Element parent = null;
    XmlPullParser pp = getXPPParser();
    pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);

    while (true) {
      int type = pp.nextToken();

      switch (type) {
        case XmlPullParser.PROCESSING_INSTRUCTION:
          {
            String text = pp.getText();
            int loc = text.indexOf(' ');

            if (loc >= 0) {
              String target = text.substring(0, loc);
              String txt = text.substring(loc + 1);
              document.addProcessingInstruction(target, txt);
            } else {
              document.addProcessingInstruction(text, "");
            }

            break;
          }

        case XmlPullParser.COMMENT:
          {
            if (parent != null) {
              parent.addComment(pp.getText());
            } else {
              document.addComment(pp.getText());
            }

            break;
          }

        case XmlPullParser.CDSECT:
          {
            if (parent != null) {
              parent.addCDATA(pp.getText());
            } else {
              String msg = "Cannot have text content outside of the " + "root document";
              throw new DocumentException(msg);
            }

            break;
          }

        case XmlPullParser.END_DOCUMENT:
          return document;

        case XmlPullParser.START_TAG:
          {
            QName qname =
                (pp.getPrefix() == null)
                    ? df.createQName(pp.getName(), pp.getNamespace())
                    : df.createQName(pp.getName(), pp.getPrefix(), pp.getNamespace());
            Element newElement = df.createElement(qname);
            int nsStart = pp.getNamespaceCount(pp.getDepth() - 1);
            int nsEnd = pp.getNamespaceCount(pp.getDepth());

            for (int i = nsStart; i < nsEnd; i++) {
              if (pp.getNamespacePrefix(i) != null) {
                newElement.addNamespace(pp.getNamespacePrefix(i), pp.getNamespaceUri(i));
              }
            }

            for (int i = 0; i < pp.getAttributeCount(); i++) {
              QName qa =
                  (pp.getAttributePrefix(i) == null)
                      ? df.createQName(pp.getAttributeName(i))
                      : df.createQName(
                          pp.getAttributeName(i),
                          pp.getAttributePrefix(i),
                          pp.getAttributeNamespace(i));
              newElement.addAttribute(qa, pp.getAttributeValue(i));
            }

            if (parent != null) {
              parent.add(newElement);
            } else {
              document.add(newElement);
            }

            parent = newElement;

            break;
          }

        case XmlPullParser.END_TAG:
          {
            if (parent != null) {
              parent = parent.getParent();
            }

            break;
          }

        case XmlPullParser.ENTITY_REF:
        case XmlPullParser.TEXT:
          {
            String text = pp.getText();

            if (parent != null) {
              parent.addText(text);
            } else {
              String msg = "Cannot have text content outside of the " + "root document";
              throw new DocumentException(msg);
            }

            break;
          }

        default:
          break;
      }
    }
  }
示例#9
0
	//改写XML文件的某一元素的值
	//strRoute为XML文件从根元素开始到该元素的父元素的路径
	//strElement为需要读取的元素
	//flag为当XML含有多个同名元素时,需要改写元素所处的序号
	public void write(String strRoute, String strElement, String strSet,
			int flag) {
		//SAXBuilder builder=new SAXBuilder();
		try {
			String str = null;
			Document doc = builder.build(xmlFileName);
			Element root = doc.getRootElement();
			Element element = root;

			StringTokenizer st = new StringTokenizer(strRoute, ":");
			str = st.nextToken();

			while (st.hasMoreTokens()) {
				str = st.nextToken();
				//mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,str);
				element = element.getChild(str);

			}

			//	mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"test :"+str);
			element = (Element) element.getParent();

			//若需要改写的不是XML文件的第一个同名元素,则获取该元素
			//方法为:将之前的元素依次移到后面,需要改写的元素前移至第一个
			/*
			 * if(flag>1) { int j=flag; while(j!=1) {
			 * 
			 * Element tmp=element.getChild(str);
			 * element.addContent(tmp.detach()); j--; } }
			 */
			element = element.getChild(str).getChild(strElement);

			//	mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"gettxt
			// "+element.getText());

			element.setText(strSet);

			//若需要改写的不是XML文件的第一个同名元素
			//上面改变了次序,需要在成功改写后恢复原有次序
			/*
			 * if(flag!=1) {
			 * 
			 * java.util.List children=element.getChildren(); Iterator
			 * iterator=children.iterator(); int count=0;
			 * while(iterator.hasNext()) { Element
			 * child=(Element)iterator.next(); count++; }
			 * 
			 * System.out.println("count"+count);
			 * 
			 * k=(count+1-flag)%count;
			 * 
			 * while(k!=0) { Element tmp=element.getChild(str);
			 * element.addContent(tmp.detach()); k--; } }
			 *  
			 */
			XMLOutputter outputter = new XMLOutputter("", false, "GB2312");
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new FileWriter(xmlFileName)));

			Document myDocument = root.getDocument();

			outputter.output(myDocument, out);
			out.close();

		} catch (JDOMException jdome) {
			jdome.printStackTrace();
//			mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, xmlFileName
//					+ " is not well-formed\n");

		} catch (IOException ioe) {
			ioe.printStackTrace();
//			mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, ioe);

		} catch (Exception e) {
			e.printStackTrace();
//			mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,
//					"write not succeed\n");

		}

	}
示例#10
0
 public static void parse(java.io.Reader input, Element context) throws java.io.IOException {
   char[] buf = new char[512];
   int len, chr = input.read();
   while (true) {
     while (chr > 0 && chr <= 32) {
       /* Skip whitespace.*/
       chr = input.read();
     }
     while (chr == '(') {
       /* Skip comments.*/
       while (chr > 0 && chr != ')') {
         chr = input.read();
       }
       if (chr == ')') {
         chr = input.read();
       }
       while (chr > 0 && chr <= 32) {
         chr = input.read();
       }
     }
     len = 0;
     while (chr > 0 && chr <= 32) {
       /* Skip whitespace.*/
       chr = input.read();
     }
     while (chr > 32) {
       /* Read token.*/
       buf[len++] = (char) chr;
       chr = input.read();
     }
     String token = new String(buf, 0, len);
     len = 0;
     while (chr > 0 && chr <= 32) {
       /* Skip whitespace.*/
       chr = input.read();
     }
     if (chr == '"') {
       /* Quote-delimited value. */
       chr = input.read();
       while (chr > 0 && chr != '"') {
         buf[len++] = (char) chr;
         chr = input.read();
       }
       if (chr == '"') {
         chr = input.read();
       }
     } else {
       /* Whitespace-delimited value.*/
       while (chr > 32) {
         buf[len++] = (char) chr;
         chr = input.read();
       }
     }
     String param = new String(buf, 0, len);
     while (!token.equals(context.getToken())) {
       if (context.getSibling() != null) {
         context = context.getSibling();
       } else {
         if (context.getParent() != null) {
           context = context.getParent();
           context.end();
         } else if (token.length() > 0) {
           throw new IllegalArgumentException("Invalid token: " + token);
         } else {
           /* Zero-length token means end of file.*/
           return;
         }
       }
     }
     context.begin(param);
     if (context.getChild() != null) {
       context = context.getChild();
     } else {
       context.end();
     }
   }
 }
 @Override
 public void endEntity() {
   element = element.getParent();
 }