Valid xml name java

How to Check If String Is a Valid Xml Element Name

How to check if string is a valid XML element name?

if (preg_match('/\A(?!XML)[a-z][\w0-9-]*/i', $subject)) # valid name
> else # invalid name
>
\A Beginning of the string
(?!XML) Negative lookahead (assert that it is impossible to match "XML")
[a-z] Match a non-digit, non-punctuation character
[\w0-9-]* Match an arbitrary number of allowed characters
/i make the whole thing case-insensitive

java — How to check if string is a valid XML element name?

If you are using Xerces XML parser, you can use the XMLChar (or XML11Char) class isValidName() method, like this:

org.apache.xerces.util.XMLChar.isValidName(String name)

There is also sample code available here for isValidName .

How to check if string is valid xml name

This is not really doable without parsing, or at least—in a limited form—without using a regular expression. Names in XML permit different characters as the first character and as second and further characters — see the Name production.

Читайте также:  Телеграмм каналы программирование python

Should you implement IsValidXmlChar without a context, i.e. just checking if the given character is a NameChar, as per the XML specification, the output of your example would be GridAttributeStuff .

So you should at least tokenize the input text to retrieve valid names, and parse the input to retrieve element names, i.e. output Grid in your example.

To check if a string is a XML name, the XmlReader class offers the IsName static method. To categorize characters in an XML text, there is the XmlCharType struct in .NET Framework as well as in .NET Core, but it’s internal.

Efficient way to determine if a string is a legal XML element name

There exists a static string VerifyName(string name) function, but it throws an exception for invalid names.

I would still prefer to use this:

try
XmlConvert.VerifyName(name);
return true;
>
catch
return false;
>

Why is it invalid to have ( or ) characters in an XML Element Name?

The answer you found lists the characters reserved in the text of an XML document, i.e. the contents of elements and the values of attributes. However, your example uses punctuation within the name of an element, which is subject to stricter limits.

The full list of allowed characters can be found in the XML specification; note that the first character of the name is even further restricted. (XML 1.1 expands the list of allowed characters slightly to reflect evolution of the Unicode standard.) The main thing to notice is that most of the common punctuation from ASCII (which would have Unicode code points below #x7f) are excluded.

It is common practice to use only names which begin with a letter, and proceed with letters, digits, underscores and hyphens, but a well-written XML parser should handle a wider range of Unicode characters should you wish to use them.

Names beginning with «xml» (in any combination of upper and lower case) are specially reserved, and names containing colons will be interpreted as using namespaces, so those should also be avoided.

Note that there is no escape mechanism for these restricted characters, you just have to design your format not to need them.

What would be a regex for valid xml names?

Do you mean XML element names? If so, no, that’s too exclusive, there are lots of valid characters that that doesn’t cover. More in the spec here and here:

NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | 
[#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] |
[#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] |
[#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] |
[#xFDF0-#xFFFD] | [#x10000-#xEFFFF]

NameChar ::= NameStartChar | "-" | "." | 7 | #xB7 |
[#x0300-#x036F] | [#x203F-#x2040]

Name ::= NameStartChar (NameChar)*

How to write a regex expression to check a valid XML element NCName in javascript?

Your ^([_]|[a-zA-Z]+[\w\W])$ pattern matches a string that is either equal to _ ( [_] ) or ( | ) is formed of 1+ letters ( [a-zA-Z]+ ) followed with any char ( [\w\W] ). So, it cannot validate the strings of the type you mention.

See the regex demo and the graph (source) below:

Sample Image

  • ^ — start of string
  • [a-zA-Z_] — a letter or _
  • [\w.-]* — 0 or more letters, digits, underscores, dots or hyphens
  • $ — end of string

How to check if a long string is a valid XML?

I’ve seen this before and the problem is that XmlDocument tries to download the DTD for the document. In your sample this is http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd which lets you open a connection but never returns anything. So a simple solution (without any type of error checking mind you) is to remove anything before the -tag like this.

WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string data = wc.DownloadString("http://1pezeshk.com/");
data = data.Remove(0, data.IndexOf("XmlDocument xml = new XmlDocument();
xml.LoadXml(data);

Edit

Browsing to http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd actully returns the DTD, but it took well over a minute to respond. Since you still won’t do DTD-validation you should really just strip this from your HTML and then try to validate it as HTML.

How to generate valid XML element name from String value in Java

Here is one possible algorithm.

(2a) If the character is a valid name character other than underscore, append it to the buffer

(2b) Otherwise, append _HHHH to the buffer where HHHH is the character code in hexadecimal.

This algorithm generates a unique name for every input string and is reversible so you can reconstruct the input string from the generated name.

Источник

Валидация XML с помощью XSD, JAXB и Spring Framework

Здравствуйте! В этой статье я хочу описать программу валидации XML с помощью Spring Framework. Наиболее очевидная область применения такой валидации — это программирование web-сервисов.

Валидация производится через преобразование XML-Java (unmarshalling) по соответствующей XSD-схеме. XML-файл считается прошедшим проверку, если преобразование XML в объект Java прошло успешно.

Проект компилируется в jar файл и запускается в командной строке. Для красоты прикручен Apache ANSI Printer, в котором можно задавать шрифт, цвет и выделение текста в cmd.

Исходный код доступен в GitHub по ссылке XmlProcessor.

1. Исходные файлы и схемы

В качестве входных данных определим 2 XML файла Address.xml и Client.xml.

   V001.000.00 
50 7 Sadovaya SPB Russia 123456

Далее определим XSD-схемы XmlValidator.xsd, ComplexTypes.xsd и SimpleTypes.xsd с
описанием контейнера CombinedType и объектов типа Address и Client:

     XSD structure   XML definition    
        The version     Address type    Client type         Apartment number    House number    Street name    City name    Country name    Postal index        The id    The name      
    V000.000.00   .6.7"/>   Int, 10 max      String, 50 max      Int, 4 max      Int, 3 max      String, 40 max      City, 40 max      Country, 30 max      Int, 10 max      

Обратите внимание на элемент в CombinedType схемы ComplexTypes.xsd. Он означает, что xml-файл должен содержать либо один элемент типа Address, либо один или несколько элементов типа Client.

2. Конфигурация Spring

В файле spring-config.xml находится описание используемых в проекте бинов Printer,
FileReader, Marshaller, XMLService. Помимо указанных бинов, определен фильтр расширений
FileNameExtensionFilter, чтобы рассматривать только xml-файлы.

        xml          com.xmlprocessor.types.CombinedType    xmlvalidator.xsd complextypes.xsd simpletypes.xsd        true           

3. Преобразование XML-Java

В точке входа приложения XmlProcessorDrv сначала считываем и проверяем аргументы
командной строки, затем через вызов методов класса Compositor создаем бины printer,
fileReader и xmlService. Далее считываем xml-файлы и выполняем валидацию файлов в
директории, указанной в CLI_OPTION_DIRECTORY.

Самое интересное происходит при вызове

xmlService.validate(xmlFiles)
 package com.xmlprocessor.main; import java.io.File; import java.util.List; import com.xmlprocessor.config.Compositor; import com.xmlprocessor.service.api.PrinterInt; import com.xmlprocessor.service.api.XmlServiceInt; import com.xmlprocessor.util.CommandLineArgs; public class XmlProcessorDrv < /** Name of the program */ private static final String PROG_NAME = XmlProcessorDrv.class.getSimpleName(); /** Version of the Program */ private static final String PROG_VERSION = "1.0 (XmlProcessor v1.000)"; /** Exit Status for OK. */ private static final int EXIT_STATUS_OK = 0; /** Exit Status for not OK. */ private static final int EXIT_STATUS_NOT_OK = -1; /** * Main entry point. * Evaluates command line args and validates provided xml files * * @param args * Command line arguments */ public static void main(String[] args) < // execution status int exitStatus; // get printer object PrinterInt printer = Compositor.getPrinter(); // read command line args CommandLineArgs cmdLineArgs = new CommandLineArgs(args); // Show version if (cmdLineArgs.hasOption(CommandLineArgs.CLI_OPTION_VERSION)) < printer.printf("%s v%s\n", PROG_NAME, PROG_VERSION); >// Show help if (cmdLineArgs.hasOption(CommandLineArgs.CLI_OPTION_HELP)) < cmdLineArgs.printHelp(PROG_NAME); >// Check if the directory name is passed in args if (!cmdLineArgs.hasOption(CommandLineArgs.CLI_OPTION_DIRECTORY)) < cmdLineArgs.printHelp(PROG_NAME); return; >String dir = cmdLineArgs.getOptionValue(CommandLineArgs.CLI_OPTION_DIRECTORY); printer.printf("\n%s %s","Folder with XML files: ", dir); List xmlFiles; XmlServiceInt xmlService = Compositor.getXmlService(); try < xmlFiles = Compositor.getFileReader().readFiles(dir); printer.bold("\n\nStart validating XML files:\n"); xmlService.validate(xmlFiles); exitStatus = EXIT_STATUS_OK; >catch (Exception ex) < printer.errorln("\n" + ex.getMessage()); exitStatus = EXIT_STATUS_NOT_OK; >System.exit(exitStatus); > // main > 

Код метода validate представлен ниже.

 . /** */ public void validate(List xmlFiles) throws Exception < int fileCount = xmlFiles.size(); File currentFile; FileInputStream fileInputStream = null; Source xmlFileSource; CombinedType combinedType; AddressType addressType; for (int count = 0; count < fileCount; count++) < currentFile = xmlFiles.get(count); printer.boldln("Current file: ").println(currentFile.getPath()); try < fileInputStream = new FileInputStream(currentFile); xmlSource = new StreamSource(fileInputStream); combinedType = (CombinedType)unmarshaller.unmarshal(xmlSource); printer.boldln("Xml file [" + currentFile.getName() + "] validation success!\n"); printer.boldln("Version: ").println(combinedType.getVersion()); addressType = combinedType.getAddress(); if (addressType != null) < printer.boldln("Address: ").println(addressType.toString()); >else if (combinedType.getClients() != null) < int i=0; for (ClientType client : combinedType.getClients()) < printer.boldln("Client").println("[" + ++i + "]" + client.toString()); >> > catch(Exception e) < printer.fatalln("Xml file [" + currentFile.getName() + "] validation error: \n" + e.getMessage()); >finally < if (fileInputStream != null) < fileInputStream.close(); >> > printer.boldln("Validating complete."); > 

Ключевое преобразование XML-Java или unmarshalling происходит в строке

combinedType = (CombinedType)unmarshaller.unmarshal(xmlSource); 

Как уже упоминалось, если удалось из XML получить java-объект типа CombinedType, то
XML признается корректным.

Unmarshaller-у должен быть заранее известен конечный объект преобразования. Для
этого с помощью JAXB создадим файлы AddressType.java, ClientType.java, CombinedType.java
В IDE Eclipse: правый клик по XSD -> Generate -> JAXB Classes…

 package com.xmlprocessor.types; import java.math.BigInteger; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; /** * 

Java class for AddressType complex type.

*/ @SuppressWarnings("restriction") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "AddressType", propOrder = < "apartment", "street", "house", "city", "country", "index" >) public class AddressType < @XmlElement(name = "Apartment", required = true) protected Integer apartment; @XmlElement(name = "House", required = true) protected BigInteger house; @XmlElement(name = "Street", required = true) protected String street; @XmlElement(name = "City", required = true) protected String city; @XmlElement(name = "Country", required = true) protected String country; @XmlElement(name = "Index") protected BigInteger index; public Integer getApartment() < return apartment; >public void setApartment(Integer value) < this.apartment = value; >public String getStreet() < return street; >public void setStreet(String value) < this.street = value; >public BigInteger getHouse() < return house; >public void setHouse(BigInteger value) < this.house = value; >public String getCity() < return city; >public void setCity(String value) < this.city = value; >public String getCountry() < return country; >public void setCountry(String value) < this.country = value; >public BigInteger getIndex() < return index; >public void setIndex(BigInteger value) < this.index = value; >public boolean isSetIndex() < return (this.index!= null); >@Override public String toString() < StringBuilder sb = new StringBuilder(); sb.append("\nApartment#: " + apartment); sb.append("\nHouse#: " + house); sb.append("\nStreet: " + street); sb.append("\nCity: " + city); sb.append("\nCountry: " + country); if (this.isSetIndex()) < sb.append("\nIndex: " + index); >return sb.toString(); > >
 package com.xmlprocessor.types; import java.math.BigInteger; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; @SuppressWarnings("restriction") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "ClientType", namespace = "http://www.tempuri.org/complextypes", propOrder = < "id", "name" >) public class ClientType < @XmlElement(name = "Id", required = true) protected BigInteger id; @XmlElement(name = "Name", required = true) protected String name; public BigInteger getId() < return id; >public void setId(BigInteger value) < this.id = value; >public String getName() < return name; >public void setName(String value) < this.name = value; >@Override public String toString() < StringBuilder sb = new StringBuilder(); sb.append("\nId: " + id); sb.append("\nName: " + name); return sb.toString(); >> 
 package com.xmlprocessor.types; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; /** * 

Java class for CombinedType complex type. */ @SuppressWarnings("restriction") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "CombinedType", propOrder = < "version", "clients", "address" >) @XmlRootElement(name = "Combined", namespace = "http://www.tempuri.org/types") public class CombinedType < @XmlElement(name = "Version", required = true) protected String version; @XmlElement(name = "Client") protected Listclients; @XmlElement(name = "Address") protected AddressType address; public String getVersion() < return version; >public void setVersion(String value) < this.version = value; >public List getClients() < if (clients == null) < clients = new ArrayList(); > return this.clients; > public AddressType getAddress() < return address; >public void setAddress(AddressType value) < this.address = value; >>

4. Демонстрация работы программы

В проект включен файл Readme с описанием доступных опций и аргументов командной
строки:

 XmlProcessor v1.0 Usage: java -jar XmlProcessorDrv [-d ] [-h] [-v] -h,--help Display this help -v Version of the program -d Folder with XML files to be validated Example: java -jar xmlProcessor.jar --help -v -d "C:\\XmlSample" 

Для запуска проекта из cmd скомпилируем jar, вызвав Maven build package на pom.xml, и в
cmd введем вышеописанную команду

java -jar xmlProcessor.jar -h -v -d «C:\XmlSample»

Директория C:\XmlSample должна содержать один или несколько файлов вида Address.xml и
Client.xml. Файлы с расширением не xml будут проигнорированы.

Пример корректной работы программы:

Программа выдаст ошибку, если длина поля превышает установленный в SimpleTypes.xsd предел, имеется опечатка в имени узла, итд. Для примера допустим, что первый элемент адреса написан как AApartment:

   V001.000.00 
50 7 Sadovaya Saint Petersburg Russia 123456

В этом случае программа выдаст ошибку:

Источник

Оцените статью